C++ Program to Find Prime Number

In this post, we will learn how to find prime numbers using C++ Programming language.

A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. For example: 27 is not a prime number because 27 = 3 x 9. While 17 is a prime number because there are only two factors of 17: 1 and 17 itself.

We will find the prime numbers by checking the number of factors of a particular number. If factors > 2 then, it’s not a prime number.

So, without further ado, let’s begin this tutorial. 

C++ Program to Find Prime Number

// C++ Program to Find Prime Number
#include <iostream>
using namespace std;

int main(){
    int num, i;
    bool flag = true;
    
    // Asking for input
    cout << "Enter any positive integer: ";
    cin >> num;
    
    for (i = 2; i < num; ++i){
        if (num % i == 0){
            flag = false;
            break;
        }
    }
    if (flag == true)
        cout << num << " is a prime number.";
    else 
        cout << num << " is not a prime number.";
    return 0;
}

Output 1

Enter any positive integer: 17
17 is a prime number.

Output 2

Enter any positive integer: 91
91 is not a prime number.

How Does This Program Work ?

    int num, i;
    bool flag = true;

In this program, we have declared two int data type variables named i and num.

    // Asking for input
    cout << "Enter any positive integer: ";
    cin >> num;

Then, the user is asked to enter a positive integer.

    for (i = 2; i < num; ++i){
        if (num % i == 0){
            flag = false;
            break;
        }
    }

Then, we check whether the entered number is divisible by any number greater than 2 or not. If there is any number which is divisible by num, then flag value changes to false.

    if (flag == true)
        cout << num << " is a prime number.";
    else 
        cout << num << " is not a prime number.";

If flag = true, then the entered number is a prime number otherwise the entered number is not a prime number. 

C++ Program to Find Prime Number Using While Loop

// C++ Program to Find Prime Number
#include <iostream>
using namespace std;

int main(){
    int num, i;
    bool flag = true;
    
    // Asking for input
    cout << "Enter any positive integer: ";
    cin >> num;
    
    i = 2;
    while(i <= num/2){
        if(num % i == 0){
            flag = false;
            break;
        }
        i++;
    }
    if (flag == true)
        cout << num << " is a prime number.";
    else 
        cout << num << " is not a prime number.";
    return 0;
}

Output 1

Enter any positive integer: 97
97 is a prime number.

Output 2

Enter any positive integer: 25
25 is not a prime number.

Conclusion

I hope after going through this post, you understand how to find prime numbers using C++ Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to guide you.

Also Read:

Leave a Comment

Your email address will not be published.