Java Program to Check Pronic Number

In the previous article, we have discussed Java Program to Check Magic 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.

Program to Check Pronic Number

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.

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

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: Java Program to Check Pronic Number 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: Java Program to Check Pronic Number 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.println("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

Method-3: Java Program to Check Pronic Number By Using User Defined Method

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.println("Enter a number : ");
        int num = scan.nextInt();
        //calling the user defined method
        //to check Pronic number or not.
        checkNumber(num);
    }

     //checkNumber() method to check pronic number
    public static void checkNumber(int num)
    {

        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: 

Enter a number : 56 
56 is a Pronic Number

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: