# C/C++ program for triangular matchstick number

Matchsticks arranged in the shape of a triangle create a pattern known as a triangle matchstick number. The quantity of matchsticks indicates a triangle's value in each line. This triangular matchstick number is the number of triangles formed when two matchsticks are arranged to form an equilateral triangle. The number of matchsticks needed to form a triangle with matchsticks is known as the triangular number.

Example:

Input : X = 3

Output : 18

Input : X = 5

Output : 45

It is essentially a triangular number extension. Three times the X-th triangular number, or (3*X*(X+1))/2, will be needed to find the matchstick for a given number X.

Example:

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

Output:

`84`

Example 2:

Let us take an example of creating a triangular matchstick number in C++.

`#include <iostream>using namespace std;// Function to calculate triangular numberint triangularNumber(int n) {    return (n * (n + 1)) / 2;}// Function to display triangular matchstick patternvoid displayMatchstickPattern(int rows) {    int totalMatchsticks = triangularNumber(rows);    int matchsticksInRow;    for (int i = 1; i <= rows; ++i) {        matchsticksInRow = triangularNumber(i);        for (int j = 0; j < matchsticksInRow; ++j) {            cout << "|";  // Display matchstick        }        cout << endl;    }    cout << "Total Matchsticks: " << totalMatchsticks << endl;}int main() {    int numberOfRows;    // Get user input for the number of rows    cout << "Enter the number of rows for the triangular matchstick pattern: ";    cin >> numberOfRows;    // Display the triangular matchstick pattern    displayMatchstickPattern(numberOfRows);    return 0;}`

Output:

`Enter the number of rows for the triangular matchstick pattern: 4||||||||||||||||||||Total Matchsticks: 10`

Explanation:

In this example, two functions are defined displayMatchstickPattern to display the triangular matchstick pattern and triangular Number to calculate the triangular number. After receiving user input regarding the number of rows, the main function calls the display function to show the pattern.

Example 3:

Let us take another example of create a triangular matchstick number in C++.

`#include <iostream>using namespace std;// Function to display triangular matchstick patternvoid displayMatchstickPattern(int rows) {    int matchstickCount = 1;  // Initialize matchstick count    for (int i = 1; i <= rows; ++i) {        for (int j = 1; j <= i; ++j) {            cout << "|";  // Display matchstick            matchstickCount++;        }        cout << endl;    }    cout << "Total Matchsticks: " << matchstickCount - 1 << endl;}int main() {    int numberOfRows;    // Get user input for the number of rows    cout << "Enter the number of rows for the triangular matchstick pattern: ";    cin >> numberOfRows;    // Display the triangular matchstick pattern    displayMatchstickPattern(numberOfRows);    return 0;}`

Output:

`Enter the number of rows for the triangular matchstick pattern: 15||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||Total Matchsticks: 120`

Explanation:

In this example, the program uses nested loops to regulate the number of rows and columns that make up the matchstick pattern in the triangle. The total number of matchsticks in the pattern is tracked using the variable matchstickCount. After that, the total number of matchsticks in the pattern is calculated, displayed, and the program prints the matchsticks.

## Conclusion:

In conclusion, the C++ examples provided show various methods for creating a triangular matchstick design depending on the number of rows that the user enters. The first example makes use of a function to compute triangular numbers using nested loops for row and matchstick display. When controlling both rows and columns, a single loop is used in the second example, which adopts a more straightforward approach. The ability to compute and show the total number of matchsticks used in the pattern is a feature shared by both programs, which effectively use matchsticks to create visually appealing triangular patterns. These examples function as basic frameworks that can be expanded or altered to meet particular needs or preferences. Programming enthusiasts can benefit from these patterns as learning exercises because they offer a valuable perspective on loop structures and basic arithmetic computations in C++.