Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

C Program to Check Whether a Number is Prime Number or Not

Prime number or not in C programming

Number is Prime Number or Not.

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.

Problem Description

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.

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 Code

#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



List of Top 100 Codes