Check if a given number is Pronic
Last Updated :
07 Jan, 2024
The numbers that can be arranged to form a rectangle are called Rectangular Numbers (also known as Pronic numbers). The first few Pronic numbers are:
0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, 462 . . . . . .
Pronic number is a number which is the product of two consecutive integers, that is, a number n is a product of x and (x+1). The task is to check and print Pronic Numbers in a range.
Examples :
Input : 6
Output : Pronic Number
Explanation: 6 = 2 * 3 i.e 6 is a product
of two consecutive integers 2 and 3.
Input :56
Output :Pronic Number
Explanation: 56 = 7 * 8 i.e 56 is a product
of two consecutive integers 7 and 8.
Input : 8
Output : Not a Pronic Number
Explanation: 8 = 2 * 4 i.e 8 is a product of
2 and 4 which are not consecutive integers.
C++
// C/C++ program to check
// if a number is pronic
#include <iostream>
#include <math.h>
using namespace std;
// function to check
// Pronic Number
bool checkPronic(int x)
{
for (int i = 0;
i <= (int)(sqrt(x));
i++)
// Checking Pronic Number
// by multiplying consecutive
// numbers
if (x == i * (i + 1))
return true;
return false;
}
// Driver Code
int main(void)
{
// Printing Pronic Numbers
// upto 200
for (int i = 0; i <= 200; i++)
if (checkPronic(i))
cout << i << " ";
return 0;
}
// This code is contributed
// by Nikita Tiwari.
Java
// Java program to check and
// Print Pronic Number upto 200
import java.io.*;
import java.util.*;
import java.math.*;
class GFG
{
// function to check Pronic Number
static boolean checkPronic(int x)
{
for (int i = 0;
i <= (int)(Math.sqrt(x));
i++)
// Checking Pronic Number
// by multiplying consecutive
// numbers
if (x == i * (i + 1))
return true;
return false;
}
// Driver Code
public static void main(String[] args)
{
// Printing Pronic
// Numbers upto 200
for (int i = 0; i <= 200; i++)
if (checkPronic(i))
System.out.print(i + " ");
}
}
// This code is contributed
// by Nikita Tiwari
Python3
# Python program to check
# and print Pronic Numbers
# upto 200
import math
# function to check
# Pronic Number
def checkPronic (x) :
i = 0
while ( i <= (int)(math.sqrt(x)) ) :
# Checking Pronic Number
# by multiplying consecutive
# numbers
if ( x == i * (i + 1)) :
return True
i = i + 1
return False
# Driver Code
# Printing Pronic
# Numbers upto 200
i = 0
while (i <= 200 ) :
if checkPronic(i) :
print(i,end=" ")
i = i + 1
# This code is contributed
# by Nikita Tiwari.
C#
// Java program to check and
// Print Pronic Number upto 200
using System;
class GFG
{
// function to check
// Pronic Number
static bool checkPronic(int x)
{
for (int i = 0;
i <= (int)(Math.Sqrt(x));
i++)
// Checking Pronic Number by
// multiplying consecutive numbers
if (x == i * (i + 1))
return true;
return false;
}
// Driver Code
public static void Main()
{
// Printing Pronic
// Numbers upto 200
for (int i = 0; i <= 200; i++)
if (checkPronic(i))
Console.Write(i + " ");
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP program to check
// if a number is pronic
// function to check
// Pronic Number
function checkPronic($x)
{
for ($i = 0;
$i <= (sqrt($x));
$i++)
// Checking Pronic Number
// by multiplying consecutive
// numbers
if ($x == $i * ($i + 1))
return true;
return false;
}
// Driver Code
// Printing Pronic
// Numbers upto 200
for ($i = 0; $i <= 200; $i++)
if (checkPronic($i))
echo $i , " ";
// This code is contributed by Ajit
?>
JavaScript
<script>
// Javascript program to check
// if a number is pronic
// function to check
// Pronic Number
function checkPronic(x)
{
for (var i = 0;
i <= parseInt(Math.sqrt(x));
i++)
// Checking Pronic Number
// by multiplying consecutive
// numbers
if (x == i * (i + 1))
return true;
return false;
}
// Driver Code
// Printing Pronic Numbers
// upto 200
for (var i = 0; i <= 200; i++)
if (checkPronic(i))
document.write(i + " ");
// This code is contributed by noob2000
</script>
Output :
0 2 6 12 20 30 42 56 72 90 110 132 156 182
Time complexity: O( n sqrt n) to check for n numbers
Auxiliary Space : O(1)
Similar Reads
Check if a given number is Pronic | Efficient Approach A pronic number is such a number which can be represented as a product of two consecutive positive integers. By multiplying these two consecutive positive integers, there can be formed a rectangle which is represented by the product or pronic number. So it is also known as Rectangular Number.The fir
5 min read
Check if a number is Full Prime A full prime number is one in which the number itself is prime and all its digits are also prime. Given a number n, check if it is Full Prime or not.Examples : Input : 53 Output : Yes Explanation: Number 53 is prime and its digits are also prime. Input : 41 Output : No Explanation: Number 41 is prim
7 min read
Check if given number is Emirp or not An Emirp Number (prime spelled backwards) is a prime number that results in a different prime when its decimal digits are reversed. Given an integer n, the task is to check whether n is an Emirp number or not.Note: This definition excludes palindromic primes, i.e., prime numbers which result in the
5 min read
Check if a number is Euler Pseudoprime Given an integer N and a base number A, the task is to check whether N is a Euler Pseudoprime to the given base A. An integer N is called Euler Pseudoprime to the base A, if A > 0 and N is an odd composite number.A and N are co-prime i.e. GCD(A, N) = 1.A(N - 1) / 2 % N is either 1 or N - 1. Examp
9 min read
Check if a Number is Palindromic Prime A palindromic prime (sometimes called a palprime) is a prime number that is also a palindromic number. Given a number n, print all palindromic primes smaller than or equal to n. For example, If n is 10, the output should be â2, 3, 5, 7'. And if n is 20, the output should be â2, 3, 5, 7, 11'.Idea is
15 min read