Java Program to Check Pronic number

Learn to write a simple java program to verify if a given number is pronic number or not.

1. what is a pronic number

A pronic number is a number which is the product of two consecutive integers, that is, a number of the form ‘n x (n + 1)’. They are also called oblong numbers, heteromecic numbers, or rectangular numbers.

For example, consider following example of number 6.

Given number is : 6

2 x 3 = 6 //6 is pronic number

The first few pronic numbers are: 0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210 etc.

2. Algorithm to determine pronic number

To find if a given number is pronic or not –

  1. Find the square root ‘K’ of given number.
  2. Multiply K with K+1. If number is equal tooriginal number, the number pronic number; else not.

3. Java Program to find pronic number

public class Main 
{ 
	public static void main(String[] args) {
		
		System.out.println("56 is pronic number " + isPronicNumber(56));
		System.out.println("57 is pronic number " + isPronicNumber(57));
	}
	
    static boolean isPronicNumber(int numberToCheck) 
    { 
    	int sqrt = (int)(Math.sqrt(numberToCheck));
    	return sqrt * (sqrt + 1) == numberToCheck ? true : false;
    } 
} 

Program output.

56 is pronic number 	true
57 is pronic number 	false

Happy Learning !!

Ref : Wikipedia

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode