C Program to Check Whether a Number is Prime or Not

Write C Program to find number is Prime number or not.

In this program we will find a number is a prime number or not with the help of for loop or if else statement. A number is considered as prime number when it satisfies the below conditions. 

  • Prime number is a number which can be divided by 1 and itself
  • A number which can not be divided by any other number other than 1 or itself is a prime number.
  • It should have only 2 factors. They are, 1 and the number itself.

Ex- Number is 13. it have only 2 factor 

  • it is divisible by 1.
  • And it is divisible by itself

So it is a prime number

Prime number or not in C programming

Working:-

Step 1.Read a “num” value to check prime or not.
Step 2.set i=1,div=0.
Step 3.if i<=num if true go to step 4, else go to step 7.
Step 4.Check the condition num%i==0 if true then evaluate step 5, else go to step 6.
Step 5.set div=div+1
Step 6.i=i+1, go to step 4
Step 7.check div, if div==2 display prime, else display not prime
Step 8.Stop

C Program:-

#include<stdio.h>
int main()
{
//initializing variables
int c,number,div=0;

//user input
printf("Enter number: ");
scanf("%d",&number);

//checking for number of divisor
for(c=1;c<=number;c++)
{
if(number%c==0)
{
div++;
}
}
//no divisors other than 1 and itself
if(div==2)
{
//display
printf("%d is a prime number",number);
}
else
{
//display
printf("%d is not a prime number",number);
}
return 0;
}

Output:-

Enter number:6
6 is not a prime number

Enter number:13
13 is a prime number

Please Login/Signup to comment