WAP (Python) to print numbers in a right triangular pattern - BoiCoder

BoiCoder

Coding Made Easy for All

Breaking

Monday 19 October 2020

WAP (Python) to print numbers in a right triangular pattern


Hey all, today we are going to WAP (Python) to print numbers in a right triangular pattern. So gear up, as we are going to do this in the best way possible!


Also, if you want to see the final code, you can get at the end of this article. But I would recommend you to be with me this whole journey as we are going to go through each and every step you need to follow to become the best coder in the world. Excited? Let's start!


Overview:

WAP (Python) to print numbers in a right triangular pattern


In this program, we are asked to make a pattern printing program in python. I hope you can see our end goal in the image above. The only input required from the user would be the number of rows he/she wants in the program.


We would be using for loops that would print numbers from 1st to the nth row and column.


*As I have already told you the basics of this program, be sure to try it on your own. Check if the solution matches!*


Understanding what we need to do:


Here I will tell step by step what you need to do.


Step 1: Getting the input from the user


As told earlier, the only input required is the 'number of rows' in the pattern. I used a variable 'n' to store this value. Also, don't forget to typecast this input to 'int' so that we can perform calculations on it.


Here's the code for the first step:


Step 2: Printing the numbers in a right triangular pattern


After getting the input, we would use for loop to print the rows. We would define another variable 'i' which stores the value of row number. It will iterate in the range (1, n+1). * 'n+1' because the range function runs from start point till end point-1.*


We can see that the number of columns in a row is also equal to the row number. For example, the first row contains 1 column and similarly, the fifth row would contain 5 columns. 


Hence, we would run another for loop inside the pre-existing for loop, to print the elements of rows (i.e., the columns). We would define another variable for that called 'j' that would contain the value of the column number. It will iterate in range (1, i+1). Why i? This is because, as discussed earlier, the value of j corresponds to that of i.


Now, in this for loop, we would write a print statement that would print the value of j. Also, we would define another parameter inside this print statement, end="". This is so that the next column prints in the same line.


After this loop ends, we have written another print command which is intentionally empty. It is to ensure that every next row prints in the next line.


Here's the code for this step:


*Note: While writing this program, you would encounter various doubts on why we wrote this or that line of code. I would recommend you to change the program and run it. It would help you in discovering the significance of each line of code. This will also help in clearing your concepts.*


So now we have found the best way to print numbers in the pattern of the right triangular pattern!


Here's the final code:


Also read: WAP (Python) to print the nth term of the Fibonacci Series


And here ends our amazing journey where we found the best way to WAP (Python) to print numbers in the right triangular pattern! I really hope you liked this article and if you did then don't forget to share it with someone in need. 


Also, don't forget to subscribe to our newsletter as we do these coding sessions regularly!


Again, thanks for reading!

No comments:

Post a Comment