Friday, May 13, 2011

Check if a number is prime or not

/**
* To check whether a number is prime or not
**/
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int n, i, p = 1;
printf("Enter any number:");
scanf("%d", &n);
/* 0 and 1 are neither a prime nor a composite number */
if((n == 0) || (n == 1)) {
printf("The number is neither prime nor composite\n");
exit(0);
}
/* 2 is a prime number */
if(n == 2) {
printf("The number is prime\n");
exit(0);
}
/* Check for all numbers other than 0, 1 and 2 */
for(i = 2; i < n/2; i++)
if(n % i == 0) {
printf("Number is a composite number\n");
p = 0;
}
if(p == 1)
printf("The number is prime\n");
return 0;
}
view raw check_prime.c hosted with ❤ by GitHub
Written by

No comments:

Post a Comment