C Program to Check if Given Number is Prime or Not

//WAP TO CHECK WHETHER THE GIVEN NUMBER IS PRIME OR NOT

#include<stdio.h>
int main()
{
	int n,i;
	int flag = 0;   //to check whether the number is prime or not

	printf("Enter the number :--> ");
	scanf("%d", &n);


	for(i = 2; i < n; i++)
	{
		if(n % 2 == 0)     //if number is divisible by i then convert flag to 1
		{
			flag = 1;
		}
    }
	if(flag == 0)
	{
		printf("%d is a prime number",n);
	}
	else
	{
		printf("Given number %d is not a prime number",n);
	}
	return 0;

}

<< Previous | Next >>


  • C++ Program to Check if Given Number is Prime or Not
  • Java Program to Check if Given Number is Prime or Not
  • Python Program to Check if Given Number is Prime or Not