Check If A Given Number Is Pronic In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Check If A Given Number Is Pronic In C++

Check If A Given Number Is Pronic In C++

BLUF: Mastering Check If A Given Number Is Pronic In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Check If A Given Number Is Pronic In C++

C++ is renowned for its efficiency. Learn how Check If A Given Number Is Pronic In C++ enables low-level control and high-performance computing in the tutorial below.

In this guide, we will explore the process of verifying whether a specified number is pronic in C++, accompanied by multiple illustrations.

The result of multiplying two sequential whole numbers is known as a pronic number, alternatively labeled as a rectangular number. Pronic numbers, or rectangular numbers, are numerical values that can be organized to create a rectangle. The initial Pronic numbers start from 0 and progress through 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, 462, and so forth.

An integer formed by multiplying two consecutive numbers together is known as a pronic number. For instance, n equals the product of x and (x+1). The objective is to validate and display a series of pronic numbers.

Example:

Example

Input  : 42
Output : Pronic Number
Explanation: 42 = 6 * 7 i.e 42 is a product
of two consecutive integers 6 and 7.
Input  : 10
Output : Not a Pronic Number
Explanation: 10 = 2 * 5 i.e 10 is a product of 
2 and 5 which are not consecutive integers.

Example:

Let's consider an example to determine whether a specified number is pronic or not in C++.

Example

#include <iostream>
using namespace std;

// Function to check if a number is pronic
bool isPronic(int num) {
    for (int i = 0; i <= num / 2; ++i) {
        // Check if i * (i + 1) equals the given number
        if (i * (i + 1) == num) {
            return true; // If pronic, return true
        }
    }
    return false; // If not pronic, return false
}

int main() {
    int number;
    
    // Input the number
    cout << "Enter a number: ";
    cin >> number;
    
    // Check if the number is pronic
    if (isPronic(number)) {
        cout << number << " is a pronic number." << endl;
    } else {
        cout << number << " is not a pronic number." << endl;
    }
    
    return 0;
}

Output:

Output

Enter a number: 110
110 is a pronic number.
Enter a number: 456
456 is not a pronic number.
Enter a number: 56
56 is a pronic number.

Explanation:

  • In this example, we define a function called isPronic that gives an integer num as input and returns the true value in the case that the number is pronic and false in the opposite case.
  • The iteration of numbers from 0 to num/2 is done within the isPronic function to see if the product of the current and the subsequent numbers equals num. If so, we indicate that num is a pronic number by returning true.
  • The main function asks the user to enter a number, checks if the entered number is pronic by calling the isPronic function, and then displays the result appropriately.
  • Example 2:

Let's explore another C++ demonstration to verify whether a specified number is pronic or not.

Example

#include <iostream>
#include <cmath>
using namespace std;

// Function to check if a number is pronic
bool isPronicNumber(int num) {
    // Loop through numbers up to the square root of num
    for (int i = 0; i <= sqrt(num); ++i) {
        // Check if the product of consecutive numbers equals num
        if (num == i * (i + 1)) {
            return true; // If pronic, return true
        }
    }
    return false; // If not pronic, return false
}

// Main function to find and print pronic numbers within a range
int main() {
    // Define the range to find pronic numbers
    int lowerBound = 0;
    int upperBound = 100;

    cout << "Pronic numbers within the range " << lowerBound << " to " << upperBound << " are:" << endl;

    // Print pronic numbers within the defined range
    for (int i = lowerBound; i <= upperBound; ++i) {
        if (isPronicNumber(i)) {
            cout << i << " ";
        }
    }

    cout << endl;
    return 0;
}

Output:

Output

Pronic numbers within the range 0 to 100 are:
0 2 6 12 20 30 42 56 72 90

Conclusion:

In summary, the C++ program provided effectively verifies whether a given number is pronic or not. By employing a straightforward algorithm that iterates through numbers up to the square root of the input, the software accurately identifies numbers that meet the pronic number criteria of being the product of two consecutive integers. Implementing code within a function promotes modularity and reusability, simplifying integration into larger software projects. To enhance code readability, comprehension, and future maintenance ease, it is advisable to include concise and clear comments. This program serves as a testament to C++ programming's adaptability in executing fundamental numerical computations and showcasing how mathematical concepts can be translated into practical code.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience