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.

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<iostream.h>
	#include<conio.h>
		
	//PRIME NUMBER PROGRAM
	void main()
	{

    	int num,i,p=0;
	     clrscr();
    	cout<<"ENTER A NUMBER:- ";
        cin>>num;

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

   		getch();
	}
	

Output:


	ENTER A NUMBER:- 7
	7 IS PRIME NUMBER.

Post Your Comment