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
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
printf("Enter number: ");
//checking for number of divisor
//no divisors other than 1 and itself
printf("%d is a prime number",number);
printf("%d is not a prime number",number);
6 is not a prime number
13 is a prime number