Home >>c programs >Prime Number program in C

Prime Number program in C

Primar Number in C

Prime number is a number that is a whole number greater than 1 and only divisible by 1 or itself. It should have only two factors, the number itself and 1.

In other words prime number can be divided by itself or 1 , it cannot be divided by other numbers.

 

The few prime numbers are: 2 3 5 7 11 13 17 19 23 …..

Let's take an example of Prime Number:

#include<stdio.h>  
int main()
{    
	int num,i,m=0,flag=0;    
	printf("Enter Your number :");    
	scanf("%d",&num);    
	m=num/2;    
	for(i=2;i<=m;i++)    
	{    
		if(num%i==0)    
		{    
		printf("Number is not prime");    
		flag=1;    
		break;    
		}    
	}    
	if(flag==0)
	{    
	printf("Number is prime");     
	}
return 0;  
}
Output :
Enter Your number :3
Number is prime
Output :
Enter Your number :4
Number is not prime

No Sidebar ads