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

Updated on December 13, 2024
Check prime number by creating a function header image

Introduction

When diving into the depths of programming in C++, one of the fundamental concepts often explored is the creation of functions for specific tasks. For beginner and intermediate programmers alike, a common exercise is to create a function that checks whether a number is a prime. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. This concept is not only important for mathematical computation in software development but also serves as an excellent practice in using functions in C++.

In this article, you will learn how to craft a robust C++ function to determine if a number is prime. Embark on a journey through understanding the purpose and creation of this function, and see practical examples that implement this function in various scenarios.

Understanding the Prime Number Checker Function

To effectively check if a number is prime, a function must perform certain checks to determine if the number can be divided evenly by any number other than 1 and itself. Here’s a systematic way to approach creating such a function in C++:

Establishing the Function Prototype

  1. Start by declaring the function prototype before the main function. This gives a clear idea about what the function does and what parameters it takes.

    c++
    bool isPrime(int num);
    

    This line declares a function isPrime that returns a boolean value true or false depending on whether the given int num is a prime number.

Implementing the Function

  1. Next, focus on the implementation part of the function. The prime number checking logic can be implemented as follows:

    c++
    bool isPrime(int num) {
        if (num <= 1) return false;
        if (num <= 3) return true;
    
        if (num % 2 == 0 || num % 3 == 0) return false;
    
        for (int i = 5; i * i <= num; i += 6) {
            if (num % i == 0 || num % (i + 2) == 0)
                return false;
        }
        return true;
    }
    
    • The function first checks if the number is less than or equal to 1. If yes, it’s not a prime.
    • If the number is less than or equal to 3, it is a prime (2 and 3 are primes).
    • The function then eliminates numbers divisible by 2 and 3.
    • Finally, it checks divisibility using a loop that skips even numbers, thus reducing the number of iterations.

Examples: Using the Prime Number Checker in C++

Example 1: Checking a Single Number

  1. Check if a specific number, such as 29, is prime using the isPrime function.

    c++
    #include<iostream>
    using namespace std;
    
    // Function declaration
    bool isPrime(int num);
    
    int main() {
        int num = 29;
        if (isPrime(num)) {
            cout << num << " is a prime number." << endl;
        } else {
            cout << num << " is not a prime number." << endl;
        }
        return 0;
    }
    

    This example checks if the number 29 is a prime number and prints out the result accordingly.

Example 2: Checking a Range of Numbers

  1. Use the isPrime function to check every number from 1 to 100 and print out the prime numbers.

    c++
    #include<iostream>
    using namespace std;
    
    // Function declaration
    bool isPrime(int num);
    
    int main() {
        for (int i = 1; i <= 100; i++) {
            if (isPrime(i)) {
                cout << i << " is a prime number." << endl;
            }
        }
        return 0;
    }
    

    Here, the loop runs from 1 to 100, checking each number to see if it's prime and printing the result when it is.

Conclusion

Understanding how to craft and utilize a prime number checker function in C++ both deepens your grasp of the language and enhances your problem-solving skills. The provided examples mark not just the application of this function, but also guide you in practical implementation for various scenarios, including checking ranges of numbers. Embrace this knowledge to incorporate such functions seamlessly into more complex programs or algorithms you might encounter in your programming journey.