+92 332 4229 857 99ProjectIdeas@Gmail.com

How to check a number whether it is prime or not (C++)


Prime or not
isPrime() takes an integer value as an input parameter and returns 1 in case if the given number is prime and 0 in case if the given number is not prime.

Code

#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include "iomanip"

using namespace std;

bool isPrime(int num)
{
       for(int i=2;i<num-1;i++)
       {
              if(num%i==0)
                     return false;
       }
return true;
}

int main()
{
       cout<<isPrime(2);cout<<endl;
       cout<<isPrime(3);cout<<endl;
       cout<<isPrime(4);cout<<endl;
       cout<<isPrime(5);cout<<endl;
       cout<<isPrime(6);cout<<endl;
       cout<<isPrime(7);cout<<endl;
       cout<<isPrime(8);cout<<endl;
       cout<<isPrime(9);cout<<endl;
      
       _getche();
       return 0;
}

Output
1
1
0
1
0
1
0
0

0 comments: