Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.

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

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
Advertisements