C Program to cheak if a number is a Prime or not

Photo by Eddie Jones on Unsplash

C Program to cheak if a number is a Prime or not

In this program inputs a number from the user and tries getting a remainder upto the said number. If a remainder is got for all the numbers it is considered a prime number.

If the user inputs 5. 5 is divided by each number upto 5 and it cheaks if a remainder is got. If no remainder is got it is not considered a prime.

// Program to cheak if a number is a prime or not
#include <stdio.h> 
void checkPrime()
{
int num, i, flag = 0;
printf("Enter a number: "); scanf("%d", &num);
// 0 and 1 are not prime numbers if (num == 0 || num == 1) 
{
printf("%d is not a prime number.\n", num); 
return;
}
for (i=2;i<=num/2;i++)
{
if (num % i == 0)
{
flag = 1; break;
}
}
if (flag == 0)
printf("%d is a prime number.\n", num); 
else printf("%d is not a prime number.\n", num);
}
int main()
{
checkPrime(); return 0;
}

Hope this blog helped you in some way.

Dont forget to Follow.

Thankyou...