Prime Number Program in C

In this section you will learn how to check whether a given number is prime or not. We are performing looping operation on it for checking a number is prime or not.

Prime Number Program

What is prime number?

A number that is divisible only by itself and 1 is called prime number.

Example of prime number

2, 3, 5, 7, 11, 13, 17 etc..

How prime number program will work?

Program of prime number in c.


	#include<stdio.h>
	#include<conio.h>
		
	//PRIME NUMBER PROGRAM
	void main()
	{

    	int num,i,p=0;
	clrscr();
    	printf("ENTER A NUMBER:- ");
        scanf("%d",&num);

    	for(i=2;i<=num/2;i++)
    	{
			if(num%i==0)
			{
			p++;
			break;
			}
		}
		if(p==0 && num!= 1)
        {
		printf("%d IS PRIME NUMBER.",num);
        }
		else
        {
		printf("%d IS NOT A PRIME NUMBER.",num);
        }

   		getch();
	}
	

Output:


	ENTER A NUMBER:- 7
    7 IS PRIME NUMBER.

Post Your Comment