Monday, June 10, 2013

Find Prime number in C

Program to find Prime number in C?


Prime Number:  Prime number is a natural number that can only be divided  by 1 or itself .

For example:
13 is a prime number because no number can divide 13 completely. 13 can only be divided by 1 or 13(itself).

Program in C:

#include<stdio.h>
int main()
{
 int i;
 int n;
 int count = 0;
 printf("enter the number \n ");
 scanf("%d",&n);
 
 for(i = 2 ; i < n ; i++)
 {
  if((n%i) == 0)
  { 
   count++;
   break;
  }
  
 }
 
 if(count == 0)
 {
  printf("%d is a prime number \n",n);
 }
 
 else
 {
  printf("%d is not a prime number \n",n);
 }
 
 return 0;                                          
}   

Output:
enter the number
12
12 is not a prime number

enter the number
13
13 is a prime number

By:  +Dixit Singla 
Post a Comment