Write a c++ program to check whether a given number is prime.
Answers
Answered by
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
English,
7 months ago
Hindi,
7 months ago
Math,
7 months ago
Environmental Sciences,
1 year ago
Computer Science,
1 year ago
English,
1 year ago