Monday, 6 February 2017

Checking a number is prime or not?


#include<iostream>
using namespace
std;
void main()
{
 int num;
 int sum = 0;
 cout << "Enter the number do you want to check: " << endl;
 cin >> num;
 for (int i = 1; i <= num; ++i) {
  if (num%i == 0) {
   sum++;
  }
 }
 if (sum == 2) {
  cout << "The number " << num << " is prime " << endl;
 }
 else {
  cout << "The number " << num << " is not prime " << endl;
 }
 system("pause");
}

No comments:

Post a Comment