Prime Number in C++

Prime Number in C++

#include<iostream>
 
using namespace std;
 
int main()
{
	int n,i,flag=1;
	cout<<"Enter any number:";
	cin>>n;
	
	for(i=2;i<=n/2;++i)
	{
		if(n%i==0)
		{
			flag=0;
			break;
		}
	}
	
	if(flag)
		cout<<"\n"<<n<<" is a Prime number";
	else
	cout<<"\n"<<n<<" is not a Prime number";
 
	return 0;
}

Output

Enter any number:7

7 is a Prime number

Leave a Comment

error: Alert: Content is protected!!