Java Program to Check Pronic Number

Program to Check Pronic Number

In this article we are going to understand what Pronic number is and how we can check whether a number is Pronic or not in Java with examples.

Pronic numbers are numbers that are product of two consecutive integers.

Example:

42: 6*7 Pronic Number
30: 5*6 Pronic Number
120: 10*12 Not Pronic Number

In the above examples the numbers 42 and 30 are Pronic numbers as their they are products of to consecutive integers. 120 is not a Pronic number.

Let’s see different ways to check pronic number.

Approach :

  1. We ask the user to enter a number which we store in a integer variable.
  2. We then use a for loop to iterate through all the numbers from 1 to num.
  3. All the consecutive integers in that range is multiplied and checked. If a product is found to be equal to the entered number, then flag is set to true.
  4. If flag==true it is a Pronic number else not.

Method-1: By Using Static Value

import java.util.Scanner;
public class PronicNumber{
    public static void main(String args[])
    {
        //A number declared
        int num = 56;

        boolean flag = false;
        //Iteates from all numbers from 1 to num 
        for(int iter=0; iter < num; iter++)
        {
            //Checks whether any consecutive number multiplies to num
            if(iter*(iter+1) == num)
            {
                flag =true;
                break;
            }
        }
        if(flag)
        {
            System.out.println(num+" is a Pronic Number");
        }
        else
        {
            System.out.println(num+" is Not a Pronic Number");
        }
    }
}
Output:

56 is a Pronic Number

Method-2: By User Input Value

import java.util.Scanner;
public class PronicNumber{
    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int num = scan.nextInt();

        boolean flag = false;
        //Iteates from all numbers from 1 to num 
        for(int iter=0; iter < num; iter++)
        {
            //Checks whether any consecutive number multiplies to num
            if(iter*(iter+1) == num)
            {
                flag =true;
                break;
            }
        }
        if(flag)
        {
            System.out.println(num+" is a Pronic Number");
        }
        else
        {
            System.out.println(num+" is Not a Pronic Number");
        }
    }
}
Output:

Case-1

Enter a number : 56 
56 is a Pronic Number

Case-2

Enter a number : 55
55 is a Pronic Number

Leave a Comment