Posted on Leave a comment

‘C’ program check if an integer is prime or not

C program check if an integer is prime or not

  1. #include <stdio.h>
  2. int main()
  3. {
  4.   int n, c;
  5.   printf(“Enter a number\n”);
  6.   scanf(“%d”, &n);
  7.   if (n == 2)
  8.     printf(“Prime number.\n”);
  9.   else
  10.   {
  11.     for (c = 2; c <= n – 1; c++)
  12.     {
  13.       if (n % c == 0)
  14.         break;
  15.     }
  16.     if (c != n)
  17.       printf(“Not prime.\n”);
  18.      else
  19.        printf(“Prime number.\n”);
  20.   }
  21.   return 0;
  22. }
Leave a Reply

Your email address will not be published. Required fields are marked *