C

How To Check Prime Number In C

In this tutorial, we are going to see how to check prime number in C. A prime number is a number that is only divisible by 1 or by itself. For example, 17 is only divisible by 17 or by itself. Other primes number 2, 3, 5, 7, 11, 13, 17…

Note: 0 and 1 are not prime numbers. 2 is the only even prime number.
 

How To Check Prime Number In C
#include <stdio.h>

int main()
{
   int p = 0, i, nbr;
   
   printf(" Enter a number: ");
   scanf("%d", &nbr);
   
   for(i=1; i<=nbr; i++)
   {
      if(nbr%i==0)
      {
         p++;
      }
   }
   if(p==2)
   {
      printf(" %d is a prime number.",nbr);
   }
   else
   {
      printf(" %d is not a prime number.",nbr);
   }
}

Output:

 Enter a number: 2
 2 is a prime number.

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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