Saturday 24 September 2011

Program to check whether given number is prime or not?


Prime Number
A prime number is a number divisible only by itself and 1.A prime number is a positive whole number that has exactly two distinct positive divisors.
A prime number is a whole number p that is strictly greater than 1, and such that if p divides a product a*b then p divides at least one of a and b.


You can check any number is prime or not using given code:


#include <stdio.h>
#include <conio.h>
int main()
{


int num,i,count=0;
printf("Enter a number: ");
scanf("%d",&num);
for(i=2;i<=num/2;i++) {
 if(num%i==0) {
count++;
break;
 }
}                                        
        if(count==0 && num!= 1)
 printf("%d is a prime number.\n",num);
else
printf("%d is not a prime number.\n",num);
getch();
return 0;
}

No comments:

Post a Comment