C++ Program to Check Prime Number By Creating a Function

#include <iostream>
using namespace std;
bool isPrime(int num);
int main(){
   int num;
   bool flag;
   cout<<"Enter any  positive number: ";
   cin>>num;
   flag = isPrime(num);
   if (flag==true)
      cout<<num<<" is a prime number";
   else
      cout<<num<<" is not a prime number";
   return 0;
}
bool isPrime(int num){
    bool flag=true;
    for(int i = 2; i <= num / 2; i++) {
       if(num % i == 0) {
          flag = false;
          break;
       }
    }
    return flag;

OUTPUT:

Enter any positive number: 9
9 is not a prime number

Leave a Comment