Check Pronic Number in C#

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

What you’ll learn

  • What a pronic number is: k * (k + 1) for nonnegative k.
  • How to test by increasing k until you match or overshoot.
  • Two C# programs: one-number check and pronic listing in a range.
  • 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 the expression k * (k + 1).
  • Use long in checks to avoid multiplication overflow.

Understanding the concept of pronic numbers

A number is pronic if it can be expressed as k * (k + 1) for integer k >= 0. Also called oblong, rectangular, or heteromecic numbers.

Pn = n × (n + 1) — for example P3 = 3 × 4 = 12. 0 is pronic because 0 = 0 × 1. 1 is not pronic.

Math note

The pronic sequence grows roughly like squares, so the number of k values to try is about sqrt(n).

Test rule

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

Quick examples

12Yes
Because
3 × 4
9No
Reason
No consecutive pair product
0Yes
Because
0 × 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 come from k >= 0.

Scan k

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

Match or overshoot

If p == n, return true; if p > n, return 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

Reference program: tests whether 12 is pronic using a safe long k-loop.

c#
using System;

class Program
{
    static bool IsPronic(int num)
    {
        if (num < 0)
            return false;

        for (long k = 0; ; k++)
        {
            long product = k * (k + 1);
            if (product == num)
                return true;
            if (product > num)
                return false;
        }
    }

    static void Main()
    {
        int number = 12;

        if (IsPronic(number))
            Console.WriteLine($"{number} is a pronic number.");
        else
            Console.WriteLine($"{number} is not a pronic number.");
    }
}
2

Pronic numbers in range 1 to 20

Reuses IsPronic inside a range loop — matches the reference range output.

c#
using System;

class Program
{
    static bool IsPronic(int num)
    {
        if (num < 0)
            return false;

        for (long k = 0; ; k++)
        {
            long product = k * (k + 1);
            if (product == num)
                return true;
            if (product > num)
                return false;
        }
    }

    static void Main()
    {
        Console.WriteLine("Pronic Numbers in the range 1 to 20:");

        for (int i = 1; i <= 20; i++)
        {
            if (IsPronic(i))
                Console.Write($"{i} ");
        }

        Console.WriteLine();
    }
}

Optimization and alternatives

k-loop (preferred). Stop once k * (k + 1) > n — about O(sqrt(n)) checks.

Sqrt shortcut. For small n, int k = (int)Math.Sqrt(num); return k * (k + 1) == num; works, but floating-point rounding makes the integer loop safer.

Bounded for-loop. for (int i = 0; i * (i + 1) <= num; i++) also works when products stay within range.

❓ 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; an integer k-loop is robust and interview-friendly.
Checking one n with the k-loop is O(sqrt(n)) time and O(1) extra space.

🔄 Input / output examples

InputPronic?Because
12Yes3 × 4
9NoNo consecutive pair
0Yes0 × 1
1NoNot a product k(k+1)

Edge cases and pitfalls

Zero

n = 0

0 = 0 × 1, so zero is pronic.

One

n = 1

Not pronic — the sequence after 0 is 2, 6, 12…

Negative

Non-pronic

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 k and k + 1.
  • 12 is pronic because 3 × 4 = 12.
  • From 1 to 20, pronic numbers are 2 6 12 20.
Did you know?

Pronic numbers are products of two consecutive integers: 0, 2, 6, 12, 20, 30… They are also called oblong or rectangular numbers.

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