Computer >> Computer tutorials >  >> Programming >> C programming

C/C++ Program for Triangular Matchstick Number?


Here we will see how to count number of matchsticks are required to make the pyramid-like below. 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.

C/C++ Program for Triangular Matchstick Number?

To solve this problem, we have to use this formula −

C/C++ Program for Triangular Matchstick Number?

Example

#include <iostream>
using namespace std;
int main(){
   int x;
   cout << "Enter the size of the base: ";
   cin >> x;
   int count = 3*x*(x+1)/2;
   cout << "Required Matchsticks: " << count;
}

Output

Enter the size of the base: 5
Required Matchsticks: 45