Pronic Number Program in Java and Python
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).
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:
/**
* 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:
/**
* 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:
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:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.