Source code:
/A Prime number is a natural number greater than 1 which has no positive divisors other than 1 and itself. Write a C code to read a number and check whether it is prime. A function isPrime() is used here.*/
#include <stdio.h>
int isPrime(int num);
int isPrime(int num)
{
int i;
for(i=2; i<=num/2; i++)
{
/* If the number is divisible by any number
other than 1 and self then it is not prime */
if(num%i == 0)
{
return 0;
}
}
return 1;
}
int main()
{
int num;
printf("Enter a number:");
scanf("%d", &num);
if(isPrime(num))
{
printf("%d is Prime Number", num);
}
else
{
printf("%d is not Prime Number", num);
}
return 0;
}
Output:
No comments:
Post a Comment