C/C++ Program for Triangular Matchstick Number?

Here we will see how to count number of matchsticks required to make a triangular pyramid. The base of the pyramid is given. So if the base is 1, it will take 3 matchsticks to make a pyramid, for base 2, 9 matchsticks are needed, for base size 3, it will take 18 matchsticks.

Base = 1 3 matchsticks Base = 2 9 matchsticks Base = 3 18 matchsticks Formula: 3 × n × (n + 1) / 2 where n = base size

Syntax

matchsticks = 3 * n * (n + 1) / 2

Where n is the base size of the triangular pyramid.

Example

This program calculates the number of matchsticks required for a triangular pyramid −

#include <stdio.h>

int main() {
    int base, matchsticks;
    
    printf("Enter the size of the base: ");
    scanf("%d", &base);
    
    matchsticks = 3 * base * (base + 1) / 2;
    
    printf("Required Matchsticks: %d\n", matchsticks);
    
    return 0;
}
Enter the size of the base: 5
Required Matchsticks: 45

How It Works

The triangular matchstick number follows the pattern where each level of the pyramid contributes to the total count. For a pyramid of base n:

  • Level 1 needs 3 matchsticks (1 triangle)
  • Level 2 needs 6 additional matchsticks (2 triangles)
  • Level 3 needs 9 additional matchsticks (3 triangles)
  • And so on...

The sum becomes: 3 + 6 + 9 + ... + 3n = 3(1 + 2 + 3 + ... + n) = 3 × n × (n + 1) / 2

Conclusion

The triangular matchstick number can be efficiently calculated using the formula 3 × n × (n + 1) / 2, where n is the base size. This formula derives from the sum of the first n natural numbers multiplied by 3.

Updated on: 2026-03-15T10:54:16+05:30

197 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements