Skip to content

Instantly share code, notes, and snippets.

@mbalayil
Created May 13, 2011 06:08
Show Gist options
  • Save mbalayil/970066 to your computer and use it in GitHub Desktop.
Save mbalayil/970066 to your computer and use it in GitHub Desktop.
To check whether 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment