C Program - Write a program to check whether a number is a prime number or not.(Using While loop)
Write a program to check whether a number is a prime number or not.
Write a program to check whether a number is a prime number or not.
If you don't know what is prime number please read form this tutorial. Prime Number & Prime Factors
#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 1:
Enter the number :5
IT IS A PRIME NUMBER
Output 2:
Enter the number :12
IT IS NOT A PRIME NUMBER
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.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.