Check Triangular Number in C#

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

What you’ll learn

  • What a triangular number means: 1 + 2 + 3 + ... + k.
  • Formula check using Tk = k(k + 1) / 2 and Math.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.Sqrt and 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.

Quick check idea

If num = Tk, then 2 × num = k(k + 1), so k ≈ √(2 × num).

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 run the same running-sum logic as the loop method.

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.

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

Pseudocode
function isTriangular(n):
  if n < 1:
    return false
  k = floor(sqrt(2 * n))
  return (k * (k + 1) / 2 == n)
1

Check a single number (formula method)

Reference program: checks whether 10 is triangular using Math.Sqrt.

c#
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.");
    }
}
2

Triangular numbers from 1 to 50 (loop method)

Uses a running-sum loop to find and print all triangular numbers in the range.

c#
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

A number is triangular if it equals 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.
Tk = k * (k + 1) / 2, where k is a positive integer.
Use the loop for clarity in interviews; use the formula with Math.Sqrt for faster checks.
O(sqrt(n)) for the additive loop; O(1) for the formula approach.

🔄 Input / output examples

InputVerdict
10triangular
7not triangular
1triangular

Edge cases and pitfalls

Handle small and invalid inputs before applying the formula or loop.

n = 1

Smallest triangular

1 is triangular because T1 = 1.

n < 1

Non-positive input

Treat as not triangular in this tutorial.

Sqrt

Floating-point rounding

Casting Math.Sqrt to int works for typical interview ranges; use integer math for very large values.

⏱️ Time and space complexity

MethodTimeExtra space
Formula check (Math.Sqrt)O(1)O(1)
Additive loop checkO(√n)O(1)

Summary

  • Idea: triangular numbers are totals of 1 + 2 + ... + k.
  • Formula: Tk = k(k + 1) / 2 enables a fast check.
  • Loop: add consecutive integers until you match or pass the target.
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