C program to check if the given number is a Prime number

C program to check if the given number is a Prime number

Prime number is a very important topic as far as the technical Interviews are concerned. A prime number is a whole number greater than 1 whose only factors are 1 and itself. Based on this logic we will write a complete program to check whether the given number is a Prime number or not.


/*
	Check if the Number is Prime
	Author 			: 		Krishna Teja G S
	Repository		:		github.com/packetprep/coding-questions
	Website			: 		packetprep.com
*/

#include

int main(){

	int number,i,flag=0;

	printf("Enter a positive integer: ");
	scanf("%d",&number);
	
	//Check if it is Prime
	for(i=2; i <= number/2;i++){
			if(number % i==0)
			{
				flag =1;
			}
		}

	if(number==1)
		printf("The number is Neither Prime nor Composite \n");
	else if(flag ==1 )
		printf("The number is Composite \n");
	else
		printf("The number is Prime \n");

}

wipro free mock test
Wipro National Qualifier Test is just a few days away. To help you crack the exam we are sharing a mock test specially designed for Wipro based on all the previous papers.