Pronic number Program in Java

Pronic number in java – In this article, we will write a java program to input a number and check whether it is a Pronic Number or HeteromecicNumber or not.

A pronic number is a number that is the product of two consecutive integers, that is, a number of the form n(n + 1). The pronic number is also called oblong, heteromecic, or rectangular.

Java Program to check pronic number

package com.demo.program;

import java.util.Scanner;

public class PronicNumber {
	
	
	   public static void main(String[] args) {
		      int num = 0;
		      // read the input 
		      Scanner scan =  new Scanner(System.in);
		      System.out.print("Please enter an integer number:: ");
		      num = scan.nextInt();

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

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

   public static boolean isPronicNumber(int num) {

	   for (int i = 0;
	             i <= (int)(Math.sqrt(num));
	             i++)
	             
	            // Checking Pronic Number
	            // by multiplying consecutive
	            // numbers
	            if (num == i * (i + 1))
	                return true;
	         
	        return false;
   }


}

Output :

Please enter an integer number:: 6
6 is a pronic number

Please enter an integer number:: 50
50 is not a pronic number

In this article, we have learned what a pronic number is and written a program to check pronic number is in java or not. You can find more coding programs for interview preparation.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

48 + = 51