Demonstrate IF statement with prime numbers - C++ Statement

C++ examples for Statement:if

Description

Demonstrate IF statement with prime numbers

Demo Code

#include <iostream>
using namespace std;
#include <process.h>             //for exit()
int main()//from   ww w  .ja  va2s  .c o m
{
   unsigned long n, j;
   cout << "Enter a number: ";
   cin >> n;                     //get number to test
   for(j=2; j <= n/2; j++)       //divide by every integer from
      if(n%j == 0)               //2 on up; if remainder is 0,
      {                       //it's divisible by j
        cout << "It's not prime; divisible by " << j << endl;
        exit(0);
      }
   cout << "It's prime\n";
   return 0;
}

Result


Related Tutorials