Inverted right angle triangle in python

In this shot, we will discuss how to generate an inverted right-angled triangle using numbers in Python.

We can print a plethora of patterns using Python. The only prerequisite to do this is a good understanding of how loops work in Python.

Here, we will be using simple for loops to generate an inverted right-angled triangle using numbers.

Description

A triangle is said to be right-angled if it has an angle equal to 90 degrees on its left side. An inverted right-angled triangle is just the inverted form of this with its vertex lying on the bottom.

To execute this using Python programming, we will be using two for loops:

  • An outer loop: To handle the number of rows.
  • An inner loop: To handle the number of columns.

Code

Let’s look at the below code snippet.

# Number of rows
rows = 5

# Loop over number of rows 
for i in range[rows+1, 0, -1]:
    
    # Nested reverse loop to handle number of columns
    for j in range[0, i-1]:
        
        # Display pattern
        print[j+1, end=' ']
    print[" "]

Explanation

  • In line 2, the input for the number of rows [i.e., length of the triangle] is taken.

  • In line 5, we create a for loop to handle the number of rows. The loop is a reversed one, i.e., it starts with the input value, and with increasing rows, the number of characters to be printed decreases.

  • In line 8, we create a nested for loop [inner loop], to handle the number of columns. This follows the same principle as above, which help together to create an inverted triangle.

  • In line 11, we have printed j+1, which results in iteration from 1 [since j + 1] to a length of [row-i] in each row. As i keeps increasing after every iteration, the number of integers keeps decreasing.

  • In line 12, we use print[], to move to the next line.

Introduction :

In this python programming tutorial, we will learn how to print an inverted right-angled triangle programmatically. A right-angled triangle has one 90 degrees angle or right angle. For an inverted right-angled triangle, this right angle will be at the top left corner. The other angles will be 45 degrees each in our example.

We will learn how to print the right-angled triangle using numbers or using any other characters. The program will ask the user to enter the height of the triangle if we are printing it using numbers. It will then print each row using numbers.

Similarly, if we are printing the triangle using other characters, it will ask the user to enter both height and character to use for the triangle.

Let me show you both of these approaches one by one :

Python 3 program to print inverted right-angled triangle using number :

#example 1
height = int[input["Enter the height of the triangle : "]]

for i in range[1,height+1]:
  for j in range[1,height - i+2]:
    print[str[j]+" ", end='']
  print[]

#example 2
height = int[input["Enter the height of the triangle : "]]
c = str[input["Enter the character you want to print the triangle : "]]

for i in range[0,height]:
  for j in range[0,height - i]:
    print[c+" ", end='']
  print[]

You can also download both of these examples from here

Output :

How does it work?

In this example, we are printing an inverted right-angled triangle using only numbers. The numbers are printed serially like 1,2,3,4,5….. If the height is 5: for the first line, we are printing 5 numbers ‘1,2,3,4,5’, for the second line, we are printing 4 numbers ‘1,2,3,4’, for the third line, three numbers ‘1,2,3’ etc.

  • First, we are taking the height of the triangle as an input from the user. The program can print a triangle of any height.
  • We are running one for loop for height number of times. Inside this loop, we will print the characters for each row.
  • For each iteration of the loop, we are running one more internal loop to print the numbers of the triangle. This is an inner for-loop. For each iteration of the outer loop, the inner loop will run.
  • The inner loop runs from j = 1 to j = height – i +2 i.e. if height is 5, for first time, it will run in range j = 1 to j = 5 – 1 + 2 = 6 or for 5 times, for the second time, it will run from j = 1 to j = 5 or for 4 times etc.
  • The inner loop prints out the numbers serially.

We can also print the same inverted triangle using any character. Let’s check :

Python 3 program to print inverted right-angled triangle using any character :

Output :

How does it work?

This example is the same as the above one. We are running two ‘for’ loops: the outer one will run for the same number of times as the height of the triangle, and the inner one will run based on the current height. Unlike the previous example, we are printing a character to create the triangle. The character is also given by the user. Here, we are using ‘*’ to create the triangle, but we can use ‘$’,’&’,’#’ etc.

Conclusion :

In this tutorial, we have learned how to print one inverted right-angled triangle in python using numbers or any other characters. This tutorial is a good example of using nested for loops in python. You can modify the program to print one non-inverted right-angle triangle. You can also try to print any other shapes using the same approach.

Try to run the example program shown above and drop one comment below if you have any queries.

Similar tutorials :

  • Python program to print a triangle using star
  • Python program to print a right angled triangle
  • Python program to find out the perimeter of a triangle
  • Python program to find the area and perimeter of a triangle
  • Python program to print a star hollow square pattern
  • Python program to print list elements in different ways

How do you reverse a right

Pattern - 2: Reverse right angle pyramid.
# This is the example of print simple reversed right angle pyramid pattern..
rows = int[input["Enter the number of rows:"]].
k = 2 * rows - 2 # It is used for number of spaces..
for i in range[0, rows]:.
for j in range[0, k]:.
print[end=" "].

How do you print the inverted triangle pattern in Python?

Programs to print triangles using *, numbers and characters First, we get the height of the pyramid rows from the user. In the first loop, we iterate from i = 0 to i = rows . The second loop runs from j = 0 to i + 1. In each iteration of this loop, we print i + 1 number of * without a new line.

How do I print inverted number pattern in Python?

We utilize the built-in function input to get the number of rows in an inverted star pattern from the user []. Because the input[] method returns a string value, we must convert the provided number to an integer using the int[] function []. Then, using a loop, we create an inverse pyramid pattern.

Chủ Đề