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 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.
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.