C Program to Find a Number is Prime or Not

Prime number is a number which is divisible by only one and itself. This article provides Source code of C Program to find Prime Number. We are declaring just two variables in this program one i used in for loop and n used for taking the user’s input. We are using modulus % operator to check whether a number is prime or not by dividing the Given number (n) with i value. If the Remainder is 0, then the number is not prime else it is prime.

C Program to find Number is Prime or Not

#include<conio.h>
#include<stdio.h>

void main()
{
	int i, n;
	clrscr();
	printf("\nEnter a Number:");
	scanf("%d",&n);
	for(i=2;i<n;i++)
	{
		if (n%i==0)
		{
			printf("\n%d is not a Prime number!",n);
			break;
		}
	}
	if(n==i)
		printf("\n%d is a Prime Number.",n);

	getch();
}

C Program to find Number is Prime or Not using Functions

#include<conio.h>
#include<stdio.h>
void checkprime(int);

void main()
{
	int n;
	clrscr();
	printf("\nEnter a Number:");
	scanf("%d",&n);
	checkprime(n);
	getch();
}

void checkprime(int n)
{
	int i;
	for(i=2;i<n;i++)
	{
		if (n%i==0)
		{
			printf("\n%d is not a Prime number!",n);
			break;
		}
	}
	if(n==i)
		printf("\n%d is a Prime Number.",n);

}

Output:

Prime-Number-using-C

Leave a Reply