C++ Program to Check if Given Number is Prime or Not using While Loop

Write C++ Program to Check if Given Number is Prime or Not using While Loop

// CPP Program to Check if Given Number is Prime or Not using While Loop

#include <iostream>

using namespace std;

int main()
{
	int n;

	cout << "Enter the number :--> ";
	cin >> n;

	int i = 2, Count=0;

	while(i < n)
	{
		if(n % i == 0)
		{
			Count++;
			break;
		}
		i++;
	}

	if(Count == 1)
	{
		cout << n << " is Not a Prime number";
	}
	else
	{
		cout << n << " is a Prime number";
	}
	return 0;
}

Output:

Enter the number :--> 11
11 Prime a number

Enter the number :--> 65
65 is Not a Prime number

≪ Previous | Next ≫