Prime Number Program in C


This article covers the prime number program in C. Here are the list of topics which was covered in this

Defination of Prime Number

A prime number is a natural number which is greater than one (1), it has no positive divisors other than one
(1) and itself.

Zero (0) and One (1) are not considered as a prime number.

Two (2) is the only even number which is prime, because remaining all even numbers are divided by two (2).

Problem Statement

Program to check whether the number is prime or not. Condition use function check() to find whether the entered
number is positive or negative if negative then re-enter the number, and if yes pass number as a parameter to
prime() and check whether no is prime or not?

  • Whether the number is positive or not, if it is negative then print the message “please enter the
    positive number”.
  • If it is positive then call the function prime and check whether the take positive number is prime or not.

Prime Number Logic to Solve

To check whether the number is prime or not, the first step is to iterate the loop starting from 2 till all the
square root of N using for loop, inside the loop it checks that every number if it divides by

If yes i.e. the number is divided by N then the entered number is not prime so we break that
loop and display the message and on another hand.

If no i.e. the number is not divided by N till completion of a loop then it does not divide any
number that is the number is prime.

We write the prime number’s main logic in isPrime() function.

Program to check whether the number is Prime or Not

void takeInput();
void check(int);
void isPrime(int);
    return 0;

    int num;



void check(int n){
    if(n < 0){

        printf(“please enter the positive number:



void isPrime(int n){
    int flag = 0, i;

    for(i = 2; i <= n / 2; i++){

        if(n%i == 0){

            flag = 1;



    if(flag == 1){

        printf(“Not Prime”);





Please enter your comment!
Please enter your name here