# Prime Number Program in C

30

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

## 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
N.

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

#include<stdio.h>
void takeInput();
void check(int);
void isPrime(int);
int
main(){
takeInput();
return 0;
}

void
takeInput(){
int num;

scanf(“%d”,&num);
check(num);

}

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

printf(“please enter the positive number:
“);
takeInput();

}
else{

isPrime(n);
}
}

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

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

if(n%i == 0){

flag = 1;

break;

}
}

if(flag == 1){

printf(“Not Prime”);

}
else{

printf(“Prime”);

}
}