Check if a given number is Pronic in C++

C++Server Side ProgrammingProgramming

Here we will see, how to check whether a number is Pronic number or not. A number that can be arranged to form a rectangle, are called the pronic numbers. First few pronic numbers are: 0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342. The pronin numbers are product of two consecutive integers. So a pronic number n = x * (x + 1).

Here we will check and generate some pronic numbers.

Example

 Live Demo

#include <iostream>
#include <cmath>
using namespace std;
bool isPronicNumber(int num) {
   for (int i = 0; i <= (int)(sqrt(num)); i++)
      if (num == i * (i + 1))
         return true;
   return false;
}
int main() {
   for (int i = 0; i <= 200; i++)
   if (isPronicNumber(i))
      cout << i << " ";
}

Output

0 2 6 12 20 30 42 56 72 90 110 132 156 182
raja
Published on 22-Oct-2019 11:15:54
Advertisements