C/C++ Program for Triangular Matchstick Number?

CC++Server Side ProgrammingProgramming

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.

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

Example

 Live Demo

#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
raja
Published on 25-Jul-2019 10:19:44
Advertisements