C program to print whether the given number is prime or not 

Here, you will learn about the way we can use the program in C to check whether the given number is prime or not. Firstly, we all know that prime numbers are the numbers which cannot be obtained by multiplying two smaller natural numbers. The first step on the code will be initializing the variables. We name the number to be tested be ‘n’. So in the code below, we will testing whether the given number ‘n’ is prime or not by dividing the number by all the numbers starting from 2 to (n-1). If the number is divisible by any of the numbers in between the range, then we call the number a prime number else the number is not a prime number. The following code will give you a simple overview of its usage.

#include<stdio.h>
int main(){
	int n,i;
	printf("Enter a number: ");
	scanf("%d",&n);
	for(i=2;i<=(n-1);i++){
		if(n%i==0){
			printf("%d is not a prime number.", n);
			break;
		}
	}
   if(n==i){
   	printf("%d is a prime number.",n);
   }
}

The above code will give an output as below:
Output:

Enter a number: 9
9 is not a prime number.

 

SHARE C program to print whether the given number is prime or not 

You may also like...

Leave a Reply

Your email address will not be published.

Share