Prime Number Identification in C++

C++C++Beginner
Practice Now

Introduction

In this lab, we will learn how to check whether a given number is prime or composite in C++. A prime number is a number greater than 1 that cannot be divided by any number other than 1 and itself. A composite number is a number greater than 1 that can be divided by numbers other than 1 and itself.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/SyntaxandStyleGroup(["`Syntax and Style`"]) cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/StandardLibraryGroup(["`Standard Library`"]) cpp(("`C++`")) -.-> cpp/ControlFlowGroup(["`Control Flow`"]) cpp(("`C++`")) -.-> cpp/FunctionsGroup(["`Functions`"]) cpp/SyntaxandStyleGroup -.-> cpp/comments("`Comments`") cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/BasicsGroup -.-> cpp/data_types("`Data Types`") cpp/BasicsGroup -.-> cpp/operators("`Operators`") cpp/StandardLibraryGroup -.-> cpp/math("`Math`") cpp/BasicsGroup -.-> cpp/booleans("`Booleans`") cpp/ControlFlowGroup -.-> cpp/conditions("`Conditions`") cpp/ControlFlowGroup -.-> cpp/for_loop("`For Loop`") cpp/FunctionsGroup -.-> cpp/function_parameters("`Function Parameters`") subgraph Lab Skills cpp/comments -.-> lab-96129{{"`Prime Number Identification in C++`"}} cpp/variables -.-> lab-96129{{"`Prime Number Identification in C++`"}} cpp/data_types -.-> lab-96129{{"`Prime Number Identification in C++`"}} cpp/operators -.-> lab-96129{{"`Prime Number Identification in C++`"}} cpp/math -.-> lab-96129{{"`Prime Number Identification in C++`"}} cpp/booleans -.-> lab-96129{{"`Prime Number Identification in C++`"}} cpp/conditions -.-> lab-96129{{"`Prime Number Identification in C++`"}} cpp/for_loop -.-> lab-96129{{"`Prime Number Identification in C++`"}} cpp/function_parameters -.-> lab-96129{{"`Prime Number Identification in C++`"}} end

Define the isPrime() Function

We will define a function isPrime(), which takes an integer and returns a boolean value representing whether the integer is prime or not.

#include <iostream>
#include <math.h>

using namespace std;

bool isPrime(int n)
{
    if (n == 1)
        return false; // 1 is not a prime number

    for (int i = 2; i <= sqrt(n); i++)
    {
        if (n % i == 0)
            return false;
    }

    return true;
}

In the above code, we are checking whether the given number is divisible by any number. If there are no such numbers, the number is prime. If the number is 1, it is not prime.

Write the main() Function

We will now write the main() function that takes input from the user and uses the isPrime() function to determine whether the given number is prime or composite.

int main()
{
    cout << "\n\nWelcome to Studytonight :-)\n\n\n";
    cout << " ===== Most Efficient Program to determine if the entered number is Prime or not. ===== \n\n";

    cout << " ===== Note: 1 is neither Prime nor Composite. ===== \n\n";

    int n;
    bool prime = false;

    cout << " Enter a positive integer other than 1 :  ";
    cin >> n;

    prime = isPrime(n);

    if (prime)
    {
        cout << "\n\nThe entered number " << n << " is a Prime number.";
    }
    else
    {
        cout << "\n\nThe entered number " << n << " is Composite number.";
    }

    cout << "\n\n\n";

    return 0;
}

Compile and Run the Program

Compile the code using the g++ command as follows:

g++ main.cpp -o main

Then run the program using the command ./main.

./main

The program will prompt you to enter a positive integer other than 1, enter a number and press enter.

The program will then output whether the entered number is prime or composite.

Full code of main.cpp

#include <iostream>
#include <math.h>

using namespace std;

bool isPrime(int n)
{
    if (n == 1)
        return false; // 1 is not a prime number

    for (int i = 2; i <= sqrt(n); i++)
    {
        if (n % i == 0)
            return false;
    }

    return true;
}

int main()
{
    cout << "\n\nWelcome to Studytonight :-)\n\n\n";
    cout << " ===== Most Efficient Program to determine if the entered number is Prime or not. ===== \n\n";

    cout << " ===== Note: 1 is neither Prime nor Composite. ===== \n\n";

    int n;
    bool prime = false;

    cout << " Enter a positive integer other than 1 :  ";
    cin >> n;

    prime = isPrime(n);

    if (prime)
    {
        cout << "\n\nThe entered number " << n << " is a Prime number.";
    }
    else
    {
        cout << "\n\nThe entered number " << n << " is Composite number.";
    }

    cout << "\n\n\n";

    return 0;
}

Summary

In this lab, we learned how to check whether a given number is prime or composite in C++. We used a function to determine if the given number was prime based on whether it was divisible by any numbers besides 1 and itself. Then, we used this function in main() which took input from the user, and output whether the number is prime or composite.

Other C++ Tutorials you may like