Java program for Pronic Number

Java program for pronic Number


According to  Wikipedia  A pronic number is a number which is the product of two consecutive integers, that is, a number of the form n(n + 1). It is also called oblong number, rectangular number or heteromecic number.
Examples : 0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, 462, 506, 552 etc

Method I


// Java program to find Pronic Number
import java.io.*;
class PronicNumber
{
    public static void main(String args[]) throws IOException
    {

        int n, i;           // n for number and i for loop
        boolean f=false;    // f for flag, initialize with false
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.print("Enter a number to find pronic or not : ");
        n = Integer.parseInt(br.readLine());
        for(i=1; i<=n; i++)
        {
            if(i*(i+1) == n)
            {
                f = true;
                break;
            }
        }
         // Checking the pronic number
        if(f == true)
            System.out.println(n+" is a Pronic Number.");
        else
            System.out.println(n+" is not a Pronic Number.");    
    }
}

Sample Output:
Enter a number to find pronic or not : 110
110 is a Pronic Number.
Enter a number to find pronic or not : 272
272 is a Pronic Number.
Enter a number to find pronic or not : 6
6 is a Pronic Number.
Enter a number to find pronic or not : 35
35 is not a Pronic Number.


Method II


// Java program to find Pronic Number
import java.io.*;
class PronicNumberWithFunction
{
    int n, i;           // n instance variable for number and i for loop
    boolean f=false;    // f instance for flag, initialize with false
    boolean pronic()throws IOException
    {
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.print("Enter a number to find pronic or not : ");
        n = Integer.parseInt(br.readLine());
        for(i=1; i<=n; i++)
        {
            if(i*(i+1) == n)
            {
                f = true;
                break;
            }
        }
        return f;
    }
    // Main method start
    public static void main(String args[]) throws IOException
    {
        // Checking the pronic number
        boolean f;
        PronicNumberWithFunction ob = new PronicNumberWithFunction();
        f = ob.pronic();
        if(f == true)
            System.out.println("A number is a Pronic Number.");
        else
            System.out.println("The number is not a Pronic Number.");    
    }
}

Sample Output:
Enter a number to find pronic or not : 12
The number is a Pronic Number.
Enter a number to find pronic or not : 130
The number is not a Pronic Number.


SHARE THIS
Previous Post
Next Post