Home / Programs / C Program - Write a program to check whether a number is a prime number or not.(Using While loop)
Programming Example

C Program - Write a program to check whether a number is a prime number or not.(Using While loop)

👁 6,067 Views
💻 Practical Program
📘 Step by Step Learning

Write a program to check whether a number is a prime number or not.

Information & Algorithm

If you don't know what is prime number please read form this tutorial. Prime Number & Prime Factors

Program Code

#include <stdio.h>
int main( )
{
	int n, r, d=2;
	printf( "\n Enter the number :");
	scanf("%d", &n);
	r = 1;
	while(d <= n/2)
	{
		r = n%d;
		if(r == 0)
			break;
		d++;
	}
	if(r==0)
		printf("\n IT IS NOT A PRIME NUMBER");
	else
		printf("\n IT IS A PRIME NUMBER");
		
	getch();	
	return 0;
}

Output

 Output 1:  

Enter the number :5

 IT IS A PRIME NUMBER



 Output 2: 

 Enter the number :12

 IT IS NOT A PRIME NUMBER 

Explanation

None

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.