Assignments » Flow Of Control » Set2 » Solution 7

Write a program to check given number is prime or not.

 

#include<iostream>
using namespace std;

int main()
{
	int n;
	bool flag=false;
	cout<<"Enter any number : ";
	cin>>n;

	for(int i=2;i<n;i++)
	{
		if(n%i==0)
		{
			flag=true;
			break;
		}
	}

	if(flag==false && n>1)
		cout<<"Number is prime";
	else
		cout<<"Number is not prime";


	return 0;
}

 

 

SAMPLE RUN # 1

Enter any number : 67
Number is prime

SAMPLE RUN # 2

Enter any number : 45
Number is not prime

 

«back