Table of contents
1.
Introduction
2.
Check Pronic Number
3.
Java Program to Check Pronic Number
4.
Pictorial Presentation
5.
Flowchart
6.
Examples
6.1.
Example 1: Checking Multiple Numbers
6.2.
Example 2: Checking Pronic Numbers in a Range
7.
Time and Space Complexity
7.1.
Time Complexity
7.2.
Space Complexity
8.
Frequently Asked Questions
8.1.
What is a Pronic number?
8.2.
How do you check if a number is Pronic in Java?
8.3.
What is the time complexity of checking a Pronic number?
9.
Conclusion
Last Updated: Mar 4, 2025
Easy

Pronic Number Program in Java

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A Pronic number (also known as a Heteromecic number) is a number that is the product of two consecutive integers, i.e., n * (n + 1). Examples of Pronic numbers include 0, 2, 6, 12, 20, etc. In  Java, you can determine whether a number is Pronic by checking if it can be expressed as the product of two consecutive integers. 

This article explains how to implement a Pronic number program in  Java.

Check Pronic Number

To check whether a number is a Pronic number, we can follow these steps:

  1. Start with an integer n.
     
  2. Loop through numbers starting from 0 and check if i * (i + 1) == n.
     
  3. If the condition is met, the number is Pronic; otherwise, it is not.

Java Program to Check Pronic Number

import java.util.Scanner;

public class PronicNumber {
    public static boolean isPronic(int num) {
        for (int i = 0; i <= Math.sqrt(num); i++) {
            if (i * (i + 1) == num) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int number = scanner.nextInt();
        scanner.close();
        
        if (isPronic(number)) {
            System.out.println(number + " is a Pronic number.");
        } else {
            System.out.println(number + " is NOT a Pronic number.");
        }
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output

Enter a number: 6
6 is a Pronic number.

Pictorial Presentation

Below is a simple representation of Pronic numbers:

0 = 0 × 1
2 = 1 × 2
6 = 2 × 3
12 = 3 × 4
20 = 4 × 5

 

This pattern continues for all Pronic numbers.

Flowchart

Flowchart

 

 

Examples

Example 1: Checking Multiple Numbers

public class PronicExample {
    public static void main(String[] args) {
        int[] numbers = {0, 2, 6, 12, 20, 30, 42, 56};
        
        for (int num : numbers) {
            if (isPronic(num)) {
                System.out.println(num + " is a Pronic number.");
            } else {
                System.out.println(num + " is NOT a Pronic number.");
            }
        }
    }
    
    public static boolean isPronic(int num) {
        for (int i = 0; i <= Math.sqrt(num); i++) {
            if (i * (i + 1) == num) {
                return true;
            }
        }
        return false;
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output

0 is a Pronic number.
2 is a Pronic number.
6 is a Pronic number.
12 is a Pronic number.
20 is a Pronic number.
30 is NOT a Pronic number.
42 is a Pronic number.
56 is a Pronic number.

Example 2: Checking Pronic Numbers in a Range

public class PronicRange {
    public static void main(String[] args) {
        System.out.println("Pronic numbers from 1 to 100: ");
        for (int i = 0; i <= 10; i++) {
            System.out.print((i * (i + 1)) + " ");
        }
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output

Pronic numbers from 1 to 100:
0 2 6 12 20 30 42 56 72 90

Time and Space Complexity

Time Complexity

  • The loop runs approximately √n times, making the time complexity O(√n).
     
  • If we check a number in a range, it will take O(N√N).

Space Complexity

  • We are using only a few variables, so the space complexity is O(1).

Frequently Asked Questions

What is a Pronic number?

A Pronic number is a number that is the product of two consecutive integers (n × (n + 1)).

How do you check if a number is Pronic in Java?

You can check by iterating from 0 to √n and verifying if i * (i + 1) == n.

What is the time complexity of checking a Pronic number?

The time complexity is O(√n) since we loop up to the square root of n.

Conclusion

In this article, we discussed on how to check if a number is a Pronic number in Java. A Pronic number (or rectangular number) is the product of two consecutive integers, represented as n × (n + 1). We implemented a  Java program to determine if a given number is Pronic. Understanding this concept helps in improving mathematical logic and problem-solving in Java programming.

Live masterclass