Check If A Given Number Is Pronic In C++

In this article, we will discuss how to check if a given number is pronic in C++ with several examples.

The product of two consecutive integers is called a pronic number , sometimes referred to as a rectangular number . Rectangular numbers (also called Pronic numbers) are numbers that can be arranged so that they form a rectangle. 0 through 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, 462...... are the inaugural Pronic numbers.

An integer that is the product of two consecutive numbers is called a pronic number . For example, n is the product of x and (x+1) . Verifying and printing a range of Pronic numbers is the task at hand.

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 us take an example to find the given 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 us take another C++ example to check if a given 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 conclusion, whether a given number is pronic or not is successfully checked by the C++ program that was presented. The software effectively determines whether a given number satisfies the definition of a pronic number, which is the product of two consecutive integers, by utilizing a simple algorithm that loops through numbers up to the square root of the input. Encapsulating code within a function allows the program to encourage code modularity and reusability, making integration into larger projects easier. In order to further improve code readability and facilitate comprehension and future maintenance, concise and clear comments are recommended. This program highlights C++ programming's flexibility in implementing basic numerical operations, as well as how a mathematical concept can be applied to the language.

Input Required

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