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 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;
}

                        

🖥 Program 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
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.