C Program to Check Given Number is Prime or not

In this tutorial you will learn about the C Program to Check Given Number is Prime or not and its application with practical example.

In this tutorial, we will learn a program to check provided number is Prime number or not using C programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • C Operators.
  • If else statement.
  • C loop statements.

What Is Prime number?

Any positive integer number which is greater than 1 and only it can be divisible by itself and by 1 and it has maximum two factors is called as prime number.Rather we can say that a prime number is a number which only divided by him or 1.

Example:-

Fact   2  is the only positive(even) and smallest integer prime number.

Program to check Prime number in C.

In this program we will find that given number is prime number or not using for loop.
Firstly we declare required header file and variable and also initiates required values in variable. Next we take value from user at run time and then after we will find that the given value is prime number or not.
Let’s take a  look to the program.

Output:-

C-Program-to-check-prime-number

In the above program, we have first declared and initialized a set variables required in the program.

  • no = number to hold given value.
  • count = count for number divisibility occurrence.
  • i= for iteration of a loop.

In the next statement user will enter the number which will be assigned to variable ‘no’. Now loop start from 2 to given number.In ever iteration we will check ‘no’ is completely divisible by ‘i’ or not.

Then we check if any number that divide completely we increase the value of count ++.
In starting we declare count=0 so in every iteration when value is completely divide we increment the value of count to one.
So , if given number is prime after the end of loop the value of count=1 however if given number is not the value of count is more than 1.
So , we will check that if number is a prime number then the value of count must be equal to one(1).

As we can see if our condition satisfy then the given number is prime number if not is not a prime number.

In this tutorial we have learn about the C Program to Check Given Number is Prime or not and its application with practical example. I hope you will like this tutorial.