What is a Pronic Number ?
A number which is the product of two consecutive numbers is a Pronic Number. Consecutive means the numbers which come one after other when counted in ascending order such as 2, 3; 5, 6; 98, 99 and so on.
Thus, a product of any two consecutive numbers is a Pronic number. Example, 12 is a product of 3 and 4 (consecutive numbers), thus it is a Pronic number.
Other examples of Pronic numbers are 20 (product of 4 and 5), 42 (product of 6 and 7), 72 (product of 8 and 9) and so on.
A Pronic number is also called Oblong number, Rectangular number or Heteromecic number.

The Problem
A number is to be taken as an input from the user and a java program should check and tell whether the input number is Pronic or not.
This can be done using any of the below approaches :
Method 1 : Looping with the number to check as the limit
Loop from 1 till the number to be checked. In each iteration of the loop, multiply the current loop counter with 1 added to it.
This is done so that we get the product of two consecutive integers. Compare this product with the number. If the product is equal to the number, the number is Pronic.
Set a boolean flag to true and terminate the loop else continue the loop with next integer.
After the end of loop check the value of boolean flag to determine whether the number is Pronic or not.

static void methodOne() {
   //initialize scanner
   Scanner scanner = new Scanner(System.in);
   System.out.println("Enter the number to check");
   //read number from user
   int number = scanner.nextInt();
   // flag which determines pronic number
   boolean isPronic = false;
   // initiate loop
   for (int counter = 1; counter <= number; counter++) {
	//multiply consecutive numbers and compare them with number
	if (counter * (counter + 1) == number) {
           //set flag to true
	   isPronic = true;
	   //no need to loop further
	   break;
	}
   }
   //check if the number is pronic
   if (isPronic) {
	System.out.println("Number is pronic");
   } else {
	System.out.println("Number is not pronic");
   }
   //close scanner
   scanner.close();
}

Output

Enter the number to check
13
Number is not pronic
Enter the number to check
12
Number is pronic

Method 2 : Taking square root of the number or by using java Math library

If the product of square root of a number and number incremented by 1 is equal to the number itself then also the number is a Pronic number. That is, if

Square of Number * (Number + 1) == Number

the given number is Pronic.

Following program is based on this logic. It read a number as input using Scanner class and calculates its square root. Since the square root may be a decimal number, it is casted to an integer to get its whole number part.
This square root is then multiplied by the number incremented by 1. The product is compared with the number. If they both are equal, the number is Pronic else not.

static void methodTwo() {
   // initialize scanner
   Scanner scanner = new Scanner(System.in);
   System.out.println("Enter the number to check");
   // read number from user
   int number = scanner.nextInt();
   //calculate square root of number using java.util.Math class
   int root = (int) Math.sqrt(number);
   // multiply consecutive numbers and compare them with number
   if (root * (root + 1) == number) {
      System.out.println("Number is pronic");
   } else {
      System.out.println("Number is not pronic");
   }
   //close scanner
   scanner.close();
}

Output

Enter the number to check
13
Number is not pronic
Enter the number to check
12
Number is pronic

Hope this post helped you out in learning something new and exciting. Hit the clap!!!

Leave a Reply