# Triangular Matchstick Number

Given a number X which represents the floor of a matchstick pyramid, write a program to print the total number of matchstick required to form pyramid of matchsticks of x floors.

Examples:

Input : X = 1 Output : 3 Input : X = 2 Output : 9

This is mainly an extension of triangular numbers. For a number X, the matchstick required will be three times of X-th triangular numbers, i.e., (3*X*(X+1))/2

## C++

`// C++ program to find X-th triangular ` `// matchstick number ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `numberOfSticks(` `int` `x) ` `{ ` ` ` `return` `(3 * x * (x + 1)) / 2; ` `} ` ` ` `int` `main() ` `{ ` ` ` `cout<<numberOfSticks(7); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find X-th triangular ` `// matchstick number ` `public` `class` `TriangularPyramidNumber { ` ` ` `public` `static` `int` `numberOfSticks(` `int` `x) ` ` ` `{ ` ` ` `return` `(` `3` `* x * (x + ` `1` `)) / ` `2` `; ` ` ` `} ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `System.out.println(numberOfSticks(` `7` `)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find X-th triangular ` `# matchstick number ` ` ` `def` `numberOfSticks(x): ` ` ` `return` `(` `3` `*` `x ` `*` `(x ` `+` `1` `)) ` `/` `2` ` ` `# main() ` `print` `(` `int` `(numberOfSticks(` `7` `))) ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find X-th triangular ` `// matchstick number ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to ind missing number ` ` ` `static` `int` `numberOfSticks(` `int` `x) ` ` ` `{ ` ` ` `return` `(3 * x * (x + 1)) / 2; ` ` ` `} ` ` ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `Console.Write(numberOfSticks(7)); ` ` ` `} ` `} ` ` ` `// This code is contributed by _omg ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find ` `// X-th triangular ` `// matchstick number ` ` ` `function` `numberOfSticks(` `$x` `) ` `{ ` ` ` `return` `(3 * ` `$x` `* (` `$x` `+ 1)) / 2; ` `} ` ` ` `// Driver code ` `echo` `(numberOfSticks(7)); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

84

## Recommended Posts:

- First triangular number whose number of divisors exceeds N
- Centered triangular number in PL/SQL
- Centered triangular number
- Smallest triangular number larger than p
- Squared triangular number (Sum of cubes)
- Program to print triangular number series till n
- Triangular Numbers
- Sum of the series 1, 3, 6, 10... (Triangular Numbers)
- Recursive program to print triangular patterns
- Program for triangular pattern (mirror image around 0)
- Program to find the Volume of a Triangular Prism
- Maximum height of triangular arrangement of array values
- Program to calculate the Surface Area of a Triangular Prism
- Icosagonal number
- Heptagonal number

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.