Tuesday, 9 February 2016

Program in C to check the number is prime or not.


Solution:
As per the definition, a positive integer which is only divisible by 1 and itself is known as prime number. 

For example: 
Let, 13 is a prime number because it is only divisible by 1 and 13 but, 15 is not prime number because it is divisible by 1, 3, 5 and 15.

Program:
#include <stdio.h>
#include<conio.h>
int main()
{
int n, i, flag=0;
printf("\n Enter a positive integer: ");
  scanf("%d", &n);
  for(i=2;i<=n/2;++i)
  {
if(n%i == 0)
      {
      flag=1;
         break;
}
  }
  if (flag == 0)
      printf("\n %d is a prime number",n);
  else
      printf("\n %d is not a prime number",n);
  return (0);
}

Output 1:
Enter a positive integer: 17
17 is not a prime number

Output 2:
Enter a positive integer: 21
21 is a prime number

No comments:

Post a Comment