Example HTML page

C Program to check the given number is a prime number or not

To find a number is a prime number or not, first we need to count the number of factors. 

The following post has a program to find the factors of a given number. In this, we took a count variable and is initialized to ‘0’. instead of printing the factors, we incremented the count for each factor. for example if the total number of factors is 2, then the given number is a prime number, otherwise, it is not a prime number. 

Click on the following link for a program to print the factors of a given number.

C Program to print the Factors of a given number

Now, the following is the code for checking a number is a prime number or not.

#include <stdio.h>
int main()
{
    int number, i,count=0;

    printf("Enter any positive integer: ");
    scanf("%d",&number);

    for(i=1; i <= number; ++i)
    {
        if (number%i == 0)
        {
            count=count+1;
        }
    }

if (count==2)
{
	printf("%d is a prime number",number);
}
else
{
	printf("%d is not a prime number",number);
}
    return 0;
}
Program to check a given number is prime or not

Output:

Command Prompt
Enter any positive integer: 24
24 is not a prime number
——————————–
Process exited after 5.36 seconds with return value 0
Press any key to continue . . .
Example HTML page

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest