C Program to find prime number

Rate this post

What is prime number?

Definition of prime number

  • A number is considered as prime number when it satisfies the below conditions.It should be whole number
    • It should be greater than 1
    • It should have only 2 factors. They are, 1 and the number itself.

For Example:

2, 3, 5, 7, 11, 13, 17, 19, 23, 29 etc

prime number
prime number

Let us see the  C program below:

#include <stdio.h>

int main()
{
   int i, num, p = 0;
   printf(“Please enter a number: \n”);
   scanf(“%d”, &num);
   for(i=1; i<=num; i++)
   {
      if(num%i==0)
      {
         p++;
      }
   }
   if(p==2)
   {
      printf(“Entered number is %d “\
             “and it is a prime number.”,num);
   }
   else
   {
      printf(“Entered number is %d “\
             “and it is not a prime number.”,num);
   }
}
Output:
Please enter a number: 17
Entered number is 17 and it is a prime number.
For any query or issue, feel free to discuss on http://discuss.eduguru.in

Leave a Reply

%d bloggers like this: