# Pronic Number Program in Java

In this post, we will develop a Java program to check whether the given number is the pronic number or not. Later we also develop a Java program to find all pronic number in a given range.

A pronic number is a number which is the product of two consecutive integers, that is, a number of the form n(n + 1). The pronic number is also called oblong numbers, heteromecic numbers, or rectangular 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 …

`````` 0 = 0 * (0+1)
2 = 1 * (1+1)
6 = 2 * (2+1)
12 = 3 * (3+1)
20 = 4 * (4+1)
30 = 5 * (5+1)
42 = 6 * (6+1)
56 = 7 * (7+1)``````

From these examples, we can conclude that when number = n*(n+1) then n will be always less than the square root of the number. We can use this conclusion for developing the Java program for pronic number.

``````import java.util.Scanner;

public class PronicNumber {

public static boolean isPronic(int number) {

int i = 0; // iterator variable

// loop until square root of the number
while(i <= (int)Math.sqrt(number)) {

if(number == i*(i+1))
return true;

// increase iterator variable by 1
i++;
}

return false;
}

public static void main(String[] args) {

// declare variables
int number = 0;

Scanner scan =  new Scanner(System.in);
System.out.print("Enter an integer number:: ");
number = scan.nextInt();

// check the number is Pronic number or not
if(isPronic(number))
System.out.println(number+" is a"
+ " pronic number");
else
System.out.println(number+" is not a"
+ " pronic number");

// close Scanner class object
scan.close();
}
}``````

The output for the different test-cases:-

Enter an integer number:: 12
12 is a pronic number

Enter an integer number:: 15
15 is not a pronic number

The time complexity of the above program is O(√n).

## Efficient way to Check Pronic Number

We can also use an efficient approach with less time complexity. We can observe that all the pronic numbers which are represented as n*(n+1), for those numbers n and n+1 value are very close to the square root value of the number. A more proper observation will lead to the fact that a number N can be represented as the product of two consecutive integers only if the product of floor(sqrt(N)) and floor(sqrt(N))+1 is equal to N.

``````import java.util.Scanner;

public class PronicNumber {

// method to check pronic number
public static boolean isPronic(int number) {

// calculate n value
int n = (int)Math.sqrt(number);

// compare n*(n+1) and number
if( n * (n+1) == number )
return true;

// else it is not a pronic number
return false;
}

public static void main(String[] args) {

// declare variables
int number = 0;

Scanner scan =  new Scanner(System.in);
System.out.print("Enter an integer number:: ");
number = scan.nextInt();

// check the number is Pronic number or not
if(isPronic(number))
System.out.println(number+" is a"
+ " pronic number");
else
System.out.println(number+" is not a"
+ " pronic number");

// close Scanner class object
scan.close();
}
}``````

The time complexity of this program is O(log(log n)).

## Java program to find all pronic number in the given range

``````import java.util.Scanner;

public class PronicNumberInRange {

// method to check pronic number
public static boolean isPronic(int number) {

// calculate n value
int n = (int)Math.sqrt(number);

// compare n*(n+1) and number
if( n * (n+1) == number )
return true;

// else it is not a pronic number
return false;
}

public static void main(String[] args) {

// declare variables
int minRange = 0, maxRange = 0;

// create Scanner class object
Scanner scan = new Scanner(System.in);

System.out.print("Enter min value of range:: ");
minRange = scan.nextInt();
System.out.print("Enter max value of range:: ");
maxRange = scan.nextInt();

// find all Pronic number
System.out.println("The pronic numbers from "+
minRange+" to "+ maxRange+" are:: ");

for(int i=minRange; i<=maxRange; i++) {
if(isPronic(i))
System.out.print(i+" ");
}

// close Scanner class object
scan.close();
}
}``````

The ouput for the different test-cases are:-

Enter min value of range:: 1
Enter max value of range:: 100
The pronic numbers from 1 to 100 are::
`2 6 12 20 30 42 56 72 90`

Enter min value of range:: 100
Enter max value of range:: 1000
The pronic numbers from 100 to 1000 are::
`110 132 156 182 210 240 272 306 342 380 420 462 506 552 600 650 702 756 812 870 930 992`

Additional properties of the pronic numbers,

1) All pronic numbers are even numbers.
2) 2 is the only prime number that is also a pronic number.
3) The nth pronic number is the sum of the first n even number.
4) If 25 is appended to the decimal representation of any pronic number, the result is a square number e.g. 625 = 25^2, 1225 = 35^2

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!