SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Pronic Number Program in Java and Python

14 August 2015

Pronic Number program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.

Question:

Write a program to input a number and check whether it is a Pronic Number or Heteromecic Number or not.

Pronic Number: A pronic number, also called an oblong number, rectangular number or heteromecic number, is a number which is the product of two consecutive integers, that is, n × (n + 1).

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, 380, 420, 462

Algorithm:

Step 1: Start.

Step 2: Accept the number n.

Step 3: Initialize a flag variable to 0.

Step 4: Initialize loop variable i to 0.

Step 5: Repeat while i * (i + 1) is less than or equal to n.

Step 6: For each i, calculate product = i * (i + 1).

Step 7: Compare product with n.

Step 8: If product equals n, set the flag to 1 and stop the loop.

Step 9: If product is smaller than n, increment i and continue.

Step 10: After the loop, display pronic if the flag is 1; otherwise display not pronic.

Step 11: Stop.

Explanation:

The program checks the definition of a pronic number directly. A number is pronic only if it can be written as the product of two consecutive integers, such as 5 × 6 = 30 or 11 × 12 = 132.

The variable flag is initialized to 0. This means that the number has not yet been proved to be pronic. The loop then tries different values of i and calculates i * (i + 1).

If the product becomes equal to the input number, the program sets flag to 1 and uses break to stop further checking. There is no need to continue the loop after the required pair of consecutive integers has been found.

After the loop, flag decides the output. If it is 1, the number is pronic. If it remains 0, no pair of consecutive integers produced the number, so it is not pronic.

The loop tests consecutive integer pairs. For each value i, the product i * (i + 1) is compared with the input number, and the loop can stop once the product crosses the input.

A pronic or heteromecic number is the product of two consecutive integers. The program checks whether the input can be written as n × (n + 1) for some integer n. A loop can multiply consecutive pairs starting from 0 and stop when the product reaches or exceeds the input. If the product equals the input, the number is pronic. If the product becomes greater, no later pair can match because the products keep increasing. This gives a clear and efficient stopping condition.

Java Program:

Java
/**
* The class PronicNumber accepts a number and checks whether it is
* a Pronic Number or Heteromecic Number.
*
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.Scanner;

class PronicNumber
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int n = sc.nextInt();

        /*
        * flag is used to remember whether a pair of consecutive
        * integers has been found whose product is n.
        */
        int flag = 0;

        /*
        * The loop checks products of consecutive numbers:
        * 0 * 1, 1 * 2, 2 * 3, 3 * 4, and so on.
        */
        for(int i = 0; i <= n; i++)
        {
            if(i * (i + 1) == n)
            {
                flag = 1;
                break;
            }
        }

        if(flag == 1)
        System.out.println(n + " is a Pronic Number");
        else
        System.out.println(n + " is not a Pronic Number");
    }
}

Alternate Java Program Using Square Root:

Java
/**
* Alternate method for checking whether a number is pronic.
* This method uses the square root to reduce the number of checks.
*/

import java.util.Scanner;

class PronicNumber
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int n = sc.nextInt();

        /*
        * If n is pronic, it will be close to k * (k + 1),
        * where k is the integer part of the square root of n.
        */
        int k = (int)(Math.sqrt(n));

        if(k * (k + 1) == n)
        System.out.println(n + " is a Pronic Number");
        else
        System.out.println(n + " is not a Pronic Number");
    }
}

Equivalent Python Program:

Python
n = int(input("Enter a number: "))

# flag is used to remember whether a suitable product is found.
flag = 0

# Check products of consecutive numbers: 0*1, 1*2, 2*3, and so on.
for i in range(0, n + 1):
    if i * (i + 1) == n:
        flag = 1
        break

# The result depends on whether the required product was found.
if flag == 1:
    print(n, "is a Pronic Number")
else:
    print(n, "is not a Pronic Number")

Output:

Enter a number: 110 110 is a Pronic Number Enter a number: 73 73 is not a Pronic Number Enter a number: 342 342 is a Pronic Number Enter a number: 15 15 is not a Pronic Number

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →