Pronic number program in Java

WAP in Java to input a number and check whether it is a Pronic Number or Heteromecic Number or not

A pronic number is a number which is the product of two consecutive integers, that is, a number of the form n(n + 1). The pronic number is also called oblong numbers, heteromecic numbers, or rectangular numbers.The first few Pronic numbers are::- 0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342

Example:

30 = 5 * (5+1)
42 = 6 * (6+1)
56 = 7 * (7+1)

import java.util.Scanner;

public class PronicNumber {

   public static boolean isPronic(int number) {

      int i = 0; // iterator variable

      // loop until square root of the number
      while(i <= (int)Math.sqrt(number)) {

	 if(number == i*(i+1))
	 return true;

	 // increase iterator variable by 1
	 i++;
      }

      return false;
   }

   public static void main(String[] args) {

      // declare variables
      int number = 0;

      // read the input 
      Scanner scan =  new Scanner(System.in);
      System.out.print("Enter an integer number:: ");
      number = scan.nextInt();

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

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

output:

Enter an integer number:: 30
30 is a pronic number
Enter an integer number:: 15
15 is not a pronic number

Leave a Reply

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