C++ Program to Check if Number is Prime or Not using For Loop
Write C++ Program to Check if Number is Prime or Not using For Loop
// CPP Program to Check if Number is Prime or Not using For Loop
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter the number :--> ";
cin >> n;
int i = 2, Count=0;
for(; 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:
Run 1:
Enter the number :--> 11
11 Prime a number
Run 2:
Enter the number :--> 65
65 is Not a Prime number