0

write a c program to find whether the given number is prime or not

26th Oct 2016, 4:39 PM
krupa varma
krupa varma - avatar
3 Answers
+ 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.
26th Oct 2016, 6:24 PM
Zen
Zen - avatar
+ 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.
27th Oct 2016, 11:15 AM
MohammadReza Dehnashi
MohammadReza Dehnashi - avatar
0
no C in here, C# only!!!
26th Oct 2016, 4:59 PM
Wolfgang Meindl
Wolfgang Meindl - avatar