Using A Function, A C++ Program Checks Prime Numbers

A prime number is one that can only be divided by one and by itself. For instance, the number 13 is a prime number since it can only be divided by itself and one, whereas the number 14 cannot be because it can also be divided by the numbers 2 and 7, in addition to the number itself and one.

This program uses a function to determine whether the supplied number is a prime number or not.

Prime number verification using a function

We’ve written a function in this program called isPrime(int), which accepts integer numbers as input and returns true or false depending on whether it finds the prime number.

The application uses the user-inputted value of num as a parameter when calling the isPrime() function. This function determines whether the value being provided in is a prime number. If it is, it returns true; otherwise, it returns false.
See: C++ Program to check prime number using loops if you’re seeking for a program that checks prime numbers without utilizing functions.

#include <iostream>
using namespace std;
bool isPrime(int num);

int main(){
   int num;
   bool flag;
   cout<<"Enter any number(should be positive integer): ";
   flag = isPrime(num);
   if (flag==true)
      cout<<num<<" is a prime number";
      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;
    return flag;


Enter any number(should be positive integer): 9
9 is not a prime number

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button