Check Triangular Number in Java

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

What you’ll learn

  • What a triangular number means: adding 1 + 2 + 3 + ... + k.
  • How to check a number using a simple loop-based running sum in Java.
  • Two complete programs: test one number and print triangular numbers from 1 to 50.
  • Live preview, edge cases, and interview-friendly complexity notes.

Overview

A triangular number is the total after adding consecutive counting numbers. This page focuses on the clean Java loop approach used in interviews and beginner classes.

Simple running-sum logic

Add 1, then 2, then 3, until you hit or pass the target.

Live preview

Try values quickly and see whether Java-style logic returns triangular or not.

Range example

Generate all triangular numbers from 1 to 50 in one pass.

Prerequisites

You only need basic Java control flow and integer variables.

  • if, while/for, and integer arithmetic in Java.
  • Understanding of positive whole numbers and sums like 1 + 2 + 3 + 4 = 10.

What is a triangular number?

A number is triangular if it equals 1 + 2 + 3 + ... + k for some positive integer k.

Example: 10 = 1 + 2 + 3 + 4, so 10 is triangular. But 7 is not, because the totals jump from 6 to 10.

Quick examples

10Triangular
Build-up
1 + 2 + 3 + 4 = 10
7Not triangular
Build-up
1 + 2 + 3 = 6, next is 10

Live preview

Enter a whole number and apply the same running-sum logic as the Java function.

Use a whole number n ≥ 1.

Live result
Press "Run check".

Algorithm

Goal: decide whether n can be written as 1 + 2 + ... + k.

Reject invalid input

If n < 1, return false.

Initialize

Set sum = 0 and k = 1.

Grow total

While sum < n, do sum += k, then k++.

Decide

If sum == n, triangular; otherwise not triangular.

📜 Pseudocode

Pseudocode
function isTriangular(n):
  if n < 1:
    return false
  sum = 0
  k = 1
  while sum < n:
    sum = sum + k
    k = k + 1
  return (sum == n)
1

Check a single number (loop method)

This program checks whether 10 is triangular.

java
public class Main {
    static boolean isTriangular(int n) {
        if (n < 1) return false;
        int sum = 0;
        int k = 1;
        while (sum < n) {
            sum += k;
            k++;
        }
        return sum == n;
    }

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

Triangular numbers from 1 to 50

This prints triangular numbers in the range 1 to 50.

java
public class Main {
    static boolean isTriangular(int n) {
        if (n < 1) return false;
        int sum = 0;
        int k = 1;
        while (sum < n) {
            sum += k;
            k++;
        }
        return sum == n;
    }

    public static void main(String[] args) {
        System.out.println("Triangular numbers from 1 to 50:");
        for (int i = 1; i <= 50; i++) {
            if (isTriangular(i)) {
                System.out.print(i + " ");
            }
        }
    }
}

Notes and optional shortcuts

Loop first. The additive loop is easiest to explain in interviews and debugging rounds.

Formula option. Triangular numbers satisfy Tk = k * (k + 1) / 2 if you need constant-time math checks.

❓ FAQ

A number is triangular if it can be written as 1 + 2 + 3 + ... + k for some positive integer k.
Yes. 10 = 1 + 2 + 3 + 4.
No. Triangular totals around 7 are 6 and 10, so 7 is not triangular.
Keep adding 1, then 2, then 3, and stop when sum reaches or exceeds n. If sum equals n, it is triangular.
O(sqrt(n)) for one check using the additive loop.

🔄 Input / output examples

InputOutput verdict
10triangular
7not triangular

Edge cases

n = 1

Smallest positive triangular number.

n < 1

Treat as non-triangular in this tutorial.

⏱️ Time and space complexity

TaskTimeExtra space
Check one number (additive loop)O(√n)O(1)

Summary

  • Idea: triangular numbers are totals of consecutive counting numbers.
  • Approach: keep adding next integers until you match or pass the target.
  • Interview tip: the loop method is easy to explain and code correctly.
Did you know?

Triangular numbers are totals of counting numbers: 1, 3, 6, 10, 15, 21, ... They represent dots arranged in triangle rows.

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