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

C Programming Language Loop control in C Language (Article) Loop control in C Language (Program)

5856

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

Program:

#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

This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.