0
write a c program to find whether the given number is prime or not
3 Respuestas
+ 2
I'm not sure in which language you want it, so I'll give you general guidelines: to test the primality of a number n, use a loop from i=2 to sqrt(n) (inclusive), where you check if n%i is 0. If you enter the if block, return false. If you exit the loop, return true.
+ 1
Example: Program to Check Prime Number in C
#include <stdio.h>
int main()
{
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=2; i<=n/2; ++i)
{
// condition for nonprime number
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
return 0;
}
Output
Enter a positive integer: 29
29 is a prime number.
0
no C in here, C# only!!!