Javatpoint Logo
Javatpoint Logo

C/C++ Program for Triangular Matchstick Number?

In this article, we will discuss the implementation of Triangle Matchstick Number in C++. But before going to its implementation, we must know about the Triangle Matchstick Number.

Introduction to Triangular Matchstick Numbers

Triangular matchstick numbers are a fun mathematical curiosity involving arranging matchsticks to form triangle shapes. The C/C++ program below generates these special numbers representing triangles made of matchsticks.

  • First, we'll create a loop that increases a counter variable "i" from 1 until it reaches a limit. During each iteration of the loop, we'll calculate the triangular number for the "i" value by adding up the numbers from 1 to "i".
  • We can accomplish this easily by introducing another loop variable, let's call it "j", which will keep track of the sum. We'll set "j" back to 1 at the beginning of each loop iteration. After that, increment it up to "i" while continuously adding its value to a variable representing the triangle.
  • After each pass through the loop, we'll print out the value of the triangular number before moving on to the "i"
  • With a line of simple code, we can generate triangular matchstick numbers for any given limit. For each pass through the loop, we calculate the i-th triangular number by summing the numbers from 1 to i.
  • An easy way to do this is with a second loop variable, j, that gets added up. At the beginning of each loop iteration, we reset the value of j to 1. After that, we increment j up to i while adding it to the triangle variable.
  • After that, at the end of each pass through the loop, we display the value of the triangular number. We repeat this process for each value of i.
  • We can generate triangular numbers using matchsticks up to any given limit with a few lines of simple code. This algorithm essentially replicates how people would physically construct larger triangles using matchsticks.
  • The output reveals a sequence: 1, 3, 6, 10, 15, 21. It confirms that these numbers align perfectly with what someone would achieve by arranging more matchsticks in triangular patterns.
  • So, while the concept of triangular matchstick numbers may seem curious, writing a program to calculate them offers a straightforward yet practical way to practice algorithms, loops, variables, and other fundamental programming concepts in C/C++.
  • The step-by-step approach mirrors the method people likely use when casually exploring these numbers using matchsticks. Additionally, the logical flow of the program demonstrates how code can replicate processes to automate patterns.

Example:

Here is a C++ program to generate triangular matchstick numbers:

Output:

Number of matchsticks for triangle 1: 1
Number of matchsticks for triangle 2: 3 
Number of matchsticks for triangle 3: 6
Number of matchsticks for triangle 4: 10
Number of matchsticks for triangle 5: 15
Number of matchsticks for triangle 6: 21 
Number of matchsticks for triangle 7: 28
Number of matchsticks for triangle 8: 36
Number of matchsticks for triangle 9: 45
Number of matchsticks for triangle 10: 55






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA