
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How To Check Whether a Number Is a Pronic Number or Not in Java?
What is Pronic number?
In mathematical terms, a Pronic number is a number that can be defined as the product of two consecutive integers. The Pronic number is in the form of n(n+1), where n is the integer.
For example, the pronic numbers are 0, 2, 6, 12, 20, and so on. It is also known asa heteromecic number, oblong number, or rectangular number.
Input & Output Scenarios
Following are the scenarios that show the mathematical calculation for checking of the pronic number:
Scenario 1
Suppose we have a number 12:
Input: 12 Output: 4 x 3 = 12
Since 12 is the product of two consecutive numbers, 3 and 4, the 12 is a pronic number.
Scenario 2
Suppose we have a number 24 to check for a pronic number:
Input: 24 Output: 6 x 4 = 24
Since the number 24 is the product of 6 and 4, but they are not consecutive numbers, 24 is not a pronic number.
Calculating the Pronic Number
Here is the explanation which will help to check a pronic number:
- First, we will calculate the square root of the given number (say inputNumber).
- Then, we will find the result of n x (n +1 ), where n is the integer part of the square root of the given number.
- Compare this result with the given number.
- If the result is equal to the given number (inputNumber), the number is a pronic number; if not, then not pronic.
Example 1
In the following example, we calculate the square root of the number 12. Using that result, we calculate n*(n+1) (where n is the square root result). Finally, we compare this result to the original number to determine whether it is a pronic number:
public class checkPronicNumber{ public static void main(String[] args){ //initialized a number long inputNumber = 12; System.out.println("Given number is: " + inputNumber); //Find square root of the input number long n = (long)Math.sqrt(inputNumber); //Check if the input number is eqaul to n(n+1) if(inputNumber == n*(n+1)){ System.out.println("Yes! " + inputNumber + " is a pronic number"); } else { System.out.println("No! " + inputNumber + " is not a pronic number"); } } }
Output
Following is the output of the above program:
Given number is: 12 Yes! 12 is a pronic number
Example 2
We define a function named pronicNumber(input number), which accepts a parameter (that will be checked for pronic number) and returns true or false based on whether the parameter is a pronic number. It uses the same logic to compare the n*(n+1) (n is the square root of 14) result with the given number 14:
public class checkPronicNumber{ //define method to check pronic number public static boolean pronicNumber(long inputNumber){ //Find square root of the input number long n = (long)Math.sqrt(inputNumber); //Check if the input number is eqaul to n(n+1) if(inputNumber == n*(n+1)){ return true; } return false; } public static void main(String[] args){ //initialized a number long inputNumber = 14; System.out.println("Given number is: " + inputNumber); //calling the method to check pronic number System.out.println("Is " + inputNumber + " is a pronic number: " + pronicNumber(inputNumber)); } }
Output
Below is the output of the above program:
Given number is: 14 Is 14 is a pronic number: false