Skip to main content

How to Check Whether a Number is Prime Number or Not in C

 How to Check Whether a Number is Prime Number or Not in C:-


  •  A number is considered as a prime number when it satisfies the below conditions. 
  • A prime number is a number that can be divided by 1 and itself
  • A number that 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.

ALGORITHMS:-

                                             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

CODE: -


#include<stdio.h>

int main()
{
    int num,i;
    printf("enter any number:");
    scanf("%d",&num);
    i=2;
    while(i<=num-1)
    {
      if(num%i==0)
      {
          printf("It is not a prime number");
          break;
      } 
         i++;
    }
      if(i==num)
         printf("prime number\n");
}


OUTPUT:-








 

 FOLLOW AND SUBSCRIBE THIS WEBPAGE FOR MORE NEW PROGRAMS AND CODING SOLUTIONS.

💬💬💬💬💗💗💗💗

Comments