Check Pronic Number in Java

Beginner
⏱️ 10 min read
📚 Updated: May 2026
🎯 2 Code Examples
Number theory

What you’ll learn

  • What a pronic number is: k * (k + 1).
  • How to test by increasing k until match or overshoot.
  • Two Java programs: one-number check and range listing.
  • Live preview behavior, edge cases, and complexity.

Overview

A pronic number is the product of two consecutive integers. Examples are 0, 2, 6, 12, 20, 30, formed by k * (k + 1) for k = 0, 1, 2, 3, 4, 5.

Consecutive product

Pattern is always k and k + 1.

Live test

Try 12 (yes) and 9 (no).

Loop stop rule

Stop when k*(k+1) exceeds n.

Prerequisites

Loops, multiplication, and simple integer comparisons.

  • Understand expression k * (k + 1).
  • Use long in checks to avoid multiplication overflow.

What is a pronic number?

A number is pronic if it can be expressed as k * (k + 1) for integer k >= 0.

0 is pronic because 0 = 0 * 1. 1 is not pronic.

Math note

Sequence grows roughly like squares, so number of checks is about sqrt(n).

Test rule

Scan k upward and stop once k*(k+1) > n.

Quick examples

12Yes
Because
3 x 4
9No
Reason
No consecutive pair product
0Yes
Because
0 x 1

Live preview

Checks whether a value equals k*(k+1) for some nonnegative integer k.

Live result
Press “Run check”.

Algorithm

Goal: decide if integer n is pronic.

Reject negatives

For this tutorial, pronic values are from k >= 0.

Scan k

Compute p = k*(k+1) for increasing k.

Match or overshoot

If p == n, true; if p > n, false.

📜 Pseudocode

Pseudocode
function isPronic(n):
  if n < 0:
    return false
  k = 0
  loop:
    p = k * (k + 1)
    if p == n: return true
    if p > n: return false
    k = k + 1
1

Check one number

java
public class Main {
    static boolean isPronic(int n) {
        if (n < 0) return false;
        for (long k = 0; ; k++) {
            long p = k * (k + 1);
            if (p == n) return true;
            if (p > n) return false;
        }
    }

    public static void main(String[] args) {
        int number = 12;
        if (isPronic(number)) {
            System.out.println(number + " is a pronic number.");
        } else {
            System.out.println(number + " is not a pronic number.");
        }
    }
}
2

Pronic numbers in range 1 to 20

java
public class Main {
    static boolean isPronic(int n) {
        if (n < 0) return false;
        for (long k = 0; ; k++) {
            long p = k * (k + 1);
            if (p == n) return true;
            if (p > n) return false;
        }
    }

    public static void main(String[] args) {
        System.out.println("Pronic numbers in the range 1 to 20:");
        for (int i = 1; i <= 20; i++) {
            if (isPronic(i)) {
                System.out.print(i + " ");
            }
        }
    }
}

Optimization notes

Stop checking once k*(k+1) > n; this yields roughly O(sqrt(n)) checks.

❓ FAQ

A pronic number can be written as k*(k+1) for some nonnegative integer k.
Yes. 0 = 0 * 1.
No. There is no integer k such that k*(k+1)=1.
Yes. 12 = 3 * 4.
Floating-point rounding can cause issues for very large values; integer loop is robust and interview-friendly.
Checking one n with k-loop is O(sqrt(n)) time and O(1) extra space.

🔄 Input / output examples

InputPronic?
12Yes
9No

Edge cases

0

0 = 0*1, so zero is pronic.

Negative

Treat negatives as non-pronic for this tutorial.

⏱️ Time and space complexity

TaskTimeExtra space
Single pronic checkO(√n)O(1)
Check all in range 1..Uabout O(U√U)O(1)

Summary

  • Pronic numbers are products of consecutive integers.
  • Check by scanning k until match or overshoot.
Did you know?

Pronic numbers are products of two consecutive integers: 0, 2, 6, 12, 20, 30...

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful