Introduction
Programmers are often encountered with a problem that is made up of mixing two or more concepts, and a good programmer knows how to approach the solution step by step. There is no need to come up with the most optimal solution first. We should first take time to build a general solution powered up by our first intuition.
To give your best in your interview at Amazon, Microsoft, or your other dream company, practice our 150 interview puzzles now, only on Coding Ninjas Studio.
Don't worry if you got stuck thinking about how to construct a Floyd triangle number pattern; remember, you either succeed or you learn. Today, we will help you learn about the Floyd Triangle pattern problem and discuss the approach to solving the problem in four different programming languages, i.e., C, C++, Java, and Python.
Floyd Triangle: Problem statement
Given the 'n' number, representing the n-number of rows (starting with row number 1), we need to construct an n-row triangle such that the triangle elements follow a gradual increase in element by one, and the number of elements in a row is equal to the current row number.
For example:
Input
n = 4
Output
1 // Row number 1
2 3 // Row number 2
4 5 6 // Row number 3
7 8 9 10 // Row number 4
Explanation
We start the series with 1 in the first row. We move to the following line because the number of elements in row 1 equals 1. Similarly, in row 2, when we get to the next two elements, we move to row number 3.
This pattern continues until the number of rows printed equals the user input of required rows.
Approach
Let's discuss the approach to solve the Floyd triangle pattern program.
Step 1: We will define an initial variable count as 0.
Step 2: we will then take the input from the user, i.e., N.
Step 3: We will iterate through the 1st loop, N number of times starting from 1.
Step 4: We will nest another loop inside, which will loop from 1 to the outer loop variable.
Step 5: We will increase the count variable by one in the inner loop.
Step 6: We will print 'count' as the last line of the inner loop.
Step 7: We will print a new line character out of the inner loop.
Now, let us discuss the above approach's code to solve the Floyd triangle number pattern problem in different languages.