
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How To Check Whether a Number Is a Pronic Number or Not in Java?
Pronic number can be defined as a number when it is a product of two consecutive integers.
Mathematically, the Pronic number is in the form of n(n+1).
It is also called as heteromecic number, oblong number or rectangular number.
To show you some instances
Instance-1
Input number is 12
Let’s check it by using the logic of pronic number −
4 * 3 = 12, where 3 and 4 are two consecutive numbers.
Hence, 12 is a pronic number.
Instance-2
Input number is 30
Let’s check it by using the logic of pronic number −
5 * 6 = 30, where 5 and 6 are two consecutive numbers.
Hence, 30 is a pronic number.
Instance-3
Input number is 81
Let’s check it by using the logic of pronic number −
9 * 9 = 81, where 9 and 9 are not two consecutive numbers.
Hence, 81 is not a pronic number.
Some other examples of pronic numbers include 0, 2, 42, 56, 90, 110, 380, 420, 462 etc.
Note −
According to Wikipedia
The only prime pronic number is 2.
All pronic numbers are even.
Syntax
To get the square root of a specified number we can use the inbuilt sqrt() method which
is present in Math class of the java.lang package.
Following is the Syntax to get the square root of the number.
(We have typecasted to long)
long squareRoot = (long)Math.sqrt(inputNumber);
Algorithm
We can follow 2 algorithms here.
Algorithm 1
Step 1 − Get an integer number either by initialization or by user input.
Step 2 − Find the square root of the input number and round it to a lower integer. Suppose it is n.
Step 3 − Find n+1.
Step 4 − Then find n(n+1)
Step 5 − If n(n+1) is equal to the original input number then it is a pronic number else it is not a pronic number.
Algorithm 2
Step 1 − Get an integer number either by initialization or by user input.
Step 2 − Take a for loop and iterate it from o to square root of the original number.
Step 3 − And inside for loop keep on checking n*(n+1)
Step 4 − If n(n+1) is equal to the original input number then it is a pronic number else it is not a pronic number.
Multiple Approaches
We have provided the solution in 2 different approaches.
By Using Static Input Value and without Loop (Algorithm-1)
By Using User Defined Method and with Loop (Algorithm-2)
Let’s see the program along with its output one by one.
Approach-1: By Using Static Input Value and without Loop
In this approach find square root say n then find n(n+1) and check if it's the same with the original number or not.
Here we have used Algorithm-1
Example
public class Main{ //main method public static void main(String[] args){ //initialized a number long inputNumber = 12; System.out.println("Given number: "); //Find square root of the input number //and assign it to a long variable say n long n = (long)Math.sqrt(inputNumber); //Check if the input number is eqaul to n(n+1) if(inputNumber==n*(n+1)){ //print it is a pronic number System.out.println(inputNumber+" is a pronic number"); } else { //else print it is not a pronic number System.out.println(inputNumber+" is not a pronic number"); } } }
Output
Given number: 12 is a pronic number
Approach-2: By Using User Defined and with Loop
In this approach the user will be asked to take the input of an integer value and then we will call a user defined method by passing this input number as parameter.
Inside the method we will check whether the number is a pronic number or not by using the algorithm.
Here we have used Algorithm-2
Example
import java.util.*; public class Main{ //main method public static void main(String[] args){ //initialized a number int inputNumber = 110; System.out.println("Given number: "+inputNumber); //calling the user defined method boolean result = checkPronic(inputNumber); if(result) //print it is a pronic number System.out.println(inputNumber+" is a pronic number"); else //else print it is not a pronic number System.out.println(inputNumber+" is not a pronic number"); } public static boolean checkPronic(int inputNumber){ //Find square root of the input number //and assign it to a long variable say n long n = (long)Math.sqrt(inputNumber); //iterate from i=0 to square root of number i.e n for(int i=0;i<=n;i++){ //if i(i+1) equals with inputNumber if(inputNumber==i*(i+1)) //return true return true; } //else return false return false; } }
Output
Given number: 110 110 is a pronic number
In this article, we explored how to check a number whether it is a pronic number or not in Java by using different approaches.
- Related Articles
- How to Check Whether a Number is Krishnamurthy Number or Not in Java?
- How To Check Whether a Number is Strontio Number or Not in Java?
- How To Check Whether a Number is Tcefrep Number or Not in Java?
- How To Check Whether a Number is Tech Number or Not in Java?
- How To Check Whether a Number Is a Bouncy Number or Not in Java?
- How To Check Whether a Number is a Evil Number or Not in Java?
- How To Check Whether a Number Is a Fascinating Number or Not in Java?
- How To Check Whether a Number is a Harshad Number or Not in Java?
- How To Check Whether a Number Is a Insolite Number or Not in Java?
- How To Check Whether a Number is a Keith Number or Not in Java?
- How To Check Whether a Number Is a Niven Number or Not in Java?
- How To Check Whether a Number is a Sunny Number or Not in Java?
- How to Check Whether a Number is a Triangular Number or Not in Java?
- How To Check Whether a Number is a Unique Number or Not in Java?
- How To Check Whether a Number is a Goldbach Number or Not in Java?
