- C program to test if a number is a power of 2
- C program to multiply two number without using multiplication(*) operator
- C program to display Fibonacci series
- C program to compare two numbers without using relational operators
- C program to convert roman number into decimal number
- C program to encrypt and decrypt the string
- C program to make calculator using switch case
- C program to convert temperature from degree celsius to fahrenheit
- Selection sort in C programming
- Binary search in C programming
- C program to sort array using bubble sort algorithm
- Linear Search in C Programming – Program and Explanation
- C program to swap two numbers
- C program to find LCM of two numbers
- C program to find GCD of two numbers
- C program to find LCM and GCD using recursion
- C program to display prime numbers between two numbers using function and without using function
- C program to check whether a string is palindrome or not
- C program to read a character from keyboard and print it in reverse case i.e if input is lower case output will be upper case and vice versa
- C program to check whether a number is prime or not
- C program to check whether a number is positive, negative or zero
- C program to check whether a number is palindrome or not
- C program to check leap year
- C program to check Armstrong number
- C program to calculate the area of square with and without using function
- C program to calculate mean using arrays
- C program to calculate area of circle

In this article, you will learn about **c program to check prime number**.

A prime number is a natural number that is divisible by 1 and itself only.

For example 2, 3, 5, 7….

Please go through following articles of C programming to understand the logic of the program.

```
#include <stdio.h>
int main()
{
int num, i, j = 0;
printf("Enter number: ");
scanf("%d", &num);
//check for prime number
for (i = 1; i <= num; i++)
{
if ((num % i) == 0)
{
j++;
}
}
if (j == 2)
printf("%d is a prime number.", num);
else
printf("%d is not a prime number.", num);
return 0;
}
```

[adsense1]

**Output**

**Explanation**

Program logic of the above program to check whether a number is prime or not is simple. We know that prime number is only divisible by 1 and itself.

Hence every time the program flow enters `for`

loop it checks whether the number is divisible by iteration number.

If the given number is perfectly divided by iteration number then `j`

is increased by `1`

. This continues till iteration reached to number itself.

Finally, the value of `j`

is checked and if it is equal to 2, the result is a prime number.