Four clear steps — start with the additive loop, then use the formula when generating.
Reject n < 1 (this page uses positive integers).
Add 1, then 2, then 3, … while the running total is less than n.
If the total lands exactly on n, it is triangular.
Or generate terms with k * (k + 1) / 2 when you need the sequence.
Method
Idea
Time
Best for
Additive loop
Sum 1..k until ≥ n
O(√n)
Interviews, showing the process
Closed formula
T_k = k(k+1)/2
O(1) per term
Generating the sequence
Perfect-square test
8n+1 is a square
O(1)
Optional fast check
Pseudocode
Pseudocode
function IsTriangular(n):
if n < 1:
return false
total = 0
k = 1
while total < n:
total = total + k
k = k + 1
return total == n
function T(k):
return k * (k + 1) / 2
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Definition
n = 1+2+…+k for some k
Formula
T_k = k(k+1)/2
First values
1, 3, 6, 10, 15, 21, 28, 36, 45
Reject
n < 1
Classic yes
10 (T_4)
Classic no
7
1..50
1 3 6 10 15 21 28 36 45
Try it
Live Preview
Enter a positive integer to see the additive chain and the triangular verdict.
Positive whole numbers. Tap a chip or type a value.
Add consecutive integers until you reach or pass the target.
Calculation
total = 0, k = 1
+1 → total = 1 (< 10)
+2 → total = 3 (< 10)
+3 → total = 6 (< 10)
+4 → total = 10 (== 10)
Stop
10 is triangular (T_4)
Formula check: 4 × 5 / 2 = 10
Code
C Programs
Start with the additive check so you can show each sum. Then list a range, and finish by generating terms with the closed formula.
Example 1 — Check One Number
Keep adding 1, 2, 3, … until the total reaches or passes n.
C
#include <stdio.h>
int is_triangular(int num)
{
int total = 0;
int k = 1;
if (num < 1)
return 0;
while (total < num)
{
total += k;
k++;
}
return total == num;
}
int main(void)
{
int number = 10;
if (is_triangular(number))
printf("%d is a triangular number.\n", number);
else
printf("%d is not a triangular number.\n", number);
return 0;
}
Output
10 is a triangular number.
How It Works
Reject non-positive inputs immediately.
Start total = 0 and k = 1.
While total < num, add k and increment k.
For 10: totals become 1, 3, 6, then 10.
Return whether the final total equals num.
Example 2 — Triangular Numbers from 1 to 50
Reuse the helper to print every triangular value in a small band.
C
#include <stdio.h>
int is_triangular(int num)
{
int total = 0;
int k = 1;
if (num < 1)
return 0;
while (total < num)
{
total += k;
k++;
}
return total == num;
}
int main(void)
{
int i;
printf("Triangular numbers in the range 1 to 50:\n");
for (i = 1; i <= 50; i++)
{
if (is_triangular(i))
printf("%d ", i);
}
printf("\n");
return 0;
}
Output
Triangular numbers in the range 1 to 50:
1 3 6 10 15 21 28 36 45
How It Works
Loop i from 1 to 50 inclusive.
Call is_triangular(i) for each value.
Print only the hits: nine numbers through 45.
This short list is a useful interview sanity check.
For longer ranges, generating with the formula is cheaper than testing every i.
Example 3 — Generate with the Closed Formula
Compute Tk = k(k+1)/2 directly for k = 1..9.
C
#include <stdio.h>
int main(void)
{
int k;
int value;
printf("Triangular numbers for k = 1 to 9:\n");
for (k = 1; k <= 9; k++)
{
value = k * (k + 1) / 2;
printf("T_%d = %d*%d/2 = %d\n", k, k, k + 1, value);
}
return 0;
}