Home / Programs / Check whether a given number is prime or not (using for loop)
Programming Example

Check whether a given number is prime or not (using for loop)

👁 1,100 Views
💻 Practical Program
📘 Step by Step Learning
A number is said to be prime if it is divisible by 1 and itself. It should not have any other divisors.

Program Code

#include<stdio.h>
void main()
{

int number, i;
int indicator =  0;
printf("Enter a number to check prime or Not: ");
scanf("%d",&number);

for(i = 2; i < number/2; i++)
{
	if(number%i == 0)
	{
	 indicator = 1;
	 break;
	}
}

if(indicator == 1){
	printf(" \n Number %d is not prime. \n", number);
}
else{
	printf(" \n Number %d is prime. \n", number);
}


}

Output

Enter a number to check prime or Not: 13

 Number 13 is prime.
<hr>
Enter a number to check prime or Not: 15

 Number 15 is not prime. 

Explanation

How to learn from this program

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.