Computer Science, asked by harsh5944, 1 year ago

Write a c++ program to check whether a given number is prime.

Answers

Answered by p1998
3

Examples: 2, 3, 13 are prime numbers.

#include <iostream>
using namespace std;

int main()
{
/* variable definition and initialization */
int n, i, c = 0;

/* Get user input */ cout << "Enter any number n: "; cin>>n;

/*logic*/ for (i = 1; i <= n; i++)
{
if (n % i == 0)
{
c++;
}
}
if (c == 2)
{
cout << "n is a Prime number" << endl;
}
else
{
cout << "n is not a Prime number" << endl;
}
return 0;
}
Similar questions