Home / Programs / C Program to check whether a given number is prime or not (using do-while loop)
🚀 Programming Example

C Program to check whether a given number is prime or not (using do-while loop)

👁 5,739 Views
💻 Practical Program
📘 Step Learning

A number is said to be prime if it is divisible by 1 and itself. It should not have any other divisors. Check whether a given number is prime or not (using do-while loop)

💻 Program Code

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

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

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

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

}
                        

🖥 Program Output

Enter a number to check prime or Not: 13

 Number 13 is prime. 
                            

📘 Explanation

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

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.

🔥 Practice suggestion

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.