- Build-up
- 1 + 2 + 3 + 4 = 10
Check Triangular Number in Java
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
- 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.
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
function isTriangular(n):
if n < 1:
return false
sum = 0
k = 1
while sum < n:
sum = sum + k
k = k + 1
return (sum == n)Check a single number (loop method)
This program checks whether 10 is triangular.
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.");
}
}
}Triangular numbers from 1 to 50
This prints triangular numbers in the range 1 to 50.
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
🔄 Input / output examples
| Input | Output verdict |
|---|---|
10 | triangular |
7 | not triangular |
Edge cases
Smallest positive triangular number.
Treat as non-triangular in this tutorial.
⏱️ Time and space complexity
| Task | Time | Extra 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.
Triangular numbers are totals of counting numbers: 1, 3, 6, 10, 15, 21, ... They represent dots arranged in triangle rows.
9 people found this page helpful
