- Build-up
- 1 + 2 + 3 + 4 = 10
Check Triangular Number in C#
What you’ll learn
- What a triangular number means:
1 + 2 + 3 + ... + k. - Formula check using
Tk = k(k + 1) / 2andMath.Sqrt. - Loop-based check and listing triangular numbers from 1 to 50.
- Live preview, edge cases, and interview-friendly complexity notes.
Overview
A triangular number is the sum of consecutive counting numbers. For example, 10 = 1 + 2 + 3 + 4. This tutorial shows both a fast formula check and a beginner-friendly loop.
Formula check
Use Math.Sqrt(2 * num) for a quick test.
Live preview
Try values like 10 or 7 instantly.
Range example
Print triangular numbers from 1 to 50.
Prerequisites
if, while/for loops, and integer arithmetic in C#.
- You understand sums like
1 + 2 + 3 + 4 = 10. - You are comfortable with
Math.Sqrtand boolean return types.
Understanding the concept of triangular numbers
A number is triangular if it equals 1 + 2 + 3 + ... + k for some positive integer k.
Example: T3 = 1 + 2 + 3 = 6 is triangular. The number 7 is not, because totals jump from 6 to 10.
Triangular number formula
The k-th triangular number is Tk = k × (k + 1) / 2, where k is a non-negative integer.
If num = Tk, then 2 × num = k(k + 1), so k ≈ √(2 × num).
Quick examples
- Build-up
- 1 + 2 + 3 = 6, next is 10
Live preview
Enter a whole number and run the same running-sum logic as the loop method.
Algorithm
Goal: decide whether n can be written as 1 + 2 + ... + k.
Reject invalid input
If n < 1, return false.
Formula path
Let k = (int)Math.Sqrt(2 * n) and test k * (k + 1) / 2 == n.
Loop path (alternative)
Add 1, 2, 3, ... until sum reaches or exceeds n; check equality.
📜 Pseudocode
function isTriangular(n):
if n < 1:
return false
k = floor(sqrt(2 * n))
return (k * (k + 1) / 2 == n) Check a single number (formula method)
Reference program: checks whether 10 is triangular using Math.Sqrt.
using System;
class Program
{
static bool IsTriangularNumber(int num)
{
if (num < 1) return false;
int k = (int)Math.Sqrt(2 * num);
return k * (k + 1) / 2 == num;
}
static void Main()
{
int number = 10;
if (IsTriangularNumber(number))
Console.WriteLine($"{number} is a triangular number.");
else
Console.WriteLine($"{number} is not a triangular number.");
}
} Triangular numbers from 1 to 50 (loop method)
Uses a running-sum loop to find and print all triangular numbers in the range.
using System;
class Program
{
static bool IsTriangular(int num)
{
if (num < 1) return false;
int sum = 0;
int k = 1;
while (sum < num)
{
sum += k;
k++;
}
return sum == num;
}
static void Main()
{
Console.WriteLine("Triangular Numbers in the Range 1 to 50:");
for (int i = 1; i <= 50; i++)
{
if (IsTriangular(i))
Console.Write(i + " ");
}
Console.WriteLine();
}
} Optimization and alternatives
Loop first. Easiest to explain in interviews and on a whiteboard.
Formula. Tk = k(k + 1) / 2 gives an O(1) check with Math.Sqrt.
Related idea. Pronic numbers use k(k + 1) without dividing by 2.
❓ FAQ
🔄 Input / output examples
| Input | Verdict |
|---|---|
10 | triangular |
7 | not triangular |
1 | triangular |
Edge cases and pitfalls
Handle small and invalid inputs before applying the formula or loop.
Smallest triangular
1 is triangular because T1 = 1.
Non-positive input
Treat as not triangular in this tutorial.
Floating-point rounding
Casting Math.Sqrt to int works for typical interview ranges; use integer math for very large values.
⏱️ Time and space complexity
| Method | Time | Extra space |
|---|---|---|
Formula check (Math.Sqrt) | O(1) | O(1) |
| Additive loop check | O(√n) | O(1) |
Summary
- Idea: triangular numbers are totals of
1 + 2 + ... + k. - Formula:
Tk = k(k + 1) / 2enables a fast check. - Loop: add consecutive integers until you match or pass the target.
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
