In this tutorial, we are going to learn how to print the right-angled triangle in Python.
Using for loop we can print the right-angled triangle. To understand this you should know the basics of for loop.
To print the right-angled triangle in Python, we can take the input from the user for the length of the triangle.
x=int[input["Enter row number=\n"]] for i in range[x]: for j in range[i+1]: print["#",end=''] print[""]
As you can see, Input is taken from the user as [x]. As we know that a for loop is used for iterating over a sequence. Then using nested for loop, you can print the right-angled triangle.
First of all, a for loop is used for row and inside that, another for loop is used for the column. The range [i+1] indicates that as the number of rows increases, the number of columns will also increase. You can print the right-angled triangle by any of the symbols.
Enter row number=4
Run the code online
As I have given the input as 4. So it will print the right-angled triangle printing the symbol [#] in 4 rows and 4 columns. Often it is done by using the “*”.
Now if we run our program, it will give the output that you can see below:
# ## ### ####
So, we did it successfully. We able to create a right angle triangle formed with “#” symbol in Python.
Also, read:
- Get the sum of all the factors of a number with Python program
Any triangle will be defined as Right Angled Triangle if it follows Pythagorus Theorem which states that sum of squares of other sides is equal to square of largest side. Like if a triangle have 3, 6, 7 as length of sides, then sum of squares of 32 + 62 = 9 + 36 = 45 which is not equal to 72 = 49. That’s why a triangle of length 3, 6, 7 is not a Right Angled
Triangle. This logic can be coded algorithmically as Python Code. Let’s see Python Code for Checking whether a Triangle is right angled or not. Output of Above Code# Checks if triangle is right angled or not using Python
a = float[input["Enter first side of triangle => "]]
b = float[input["Enter second side of triangle => "]]
c = float[input["Enter third side of triangle => "]]
# Checks which side out of three a, b and c is largest
if [a >= b] and [a >= c]:
largest_triangle_side = a
elif [b >= c] and [b >= a]:
largest_triangle_side = b
else:
largest_triangle_side = c
# Applying Pythagorean theorem to check if triangle is Right Angled
# If a is largest side of triangle
if [largest_triangle_side == a]:
if [b**2 + c**2 == a**2]:
print["Triangle is Right Angled"]
else:
print["Triangle is Not Right Angled"]
# If b is largest side of triangle
if[largest_triangle_side == b]:
if[c**2 + a**2 == b**2]:
print["Triangle is Right Angled"]
else:
print["Triangle is Not Right Angled"]
# If c is largest side of triangle
if[largest_triangle_side == c]:
if[a**2 + b**2 == c**2]:
print["Triangle is Right Angled"]
else:
print["Triangle is Not Right Angled"]
Enter first side of triangle => 1
Enter second side of triangle => 2
Enter third side of triangle => 3
Triangle is Not Right Angled
In this shot, we will discuss how to generate a right-angled triangle using numbers in Python.
We can print a plethora of patterns using Python. The basic and only prerequisite is a good understanding of how loops work in Python. Here, we will be using
simple for
loops to generate a right-angled triangle using stars and numbers.
Description
A triangle is said to be right-angled if and only if it has one angle equal to 90 degrees.
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 code snippet below to understand it better.
# Number of rows
rows = 5
# Outer loop to handle the rows
for i in range[rows]:
# Inner loop to handle the columns
for j in range[i + 1]:
# Printing the pattern
print[j+1, end=' ']
# Next Line
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.In line 8, we create a nested
for
loop [inner loop], to handle the number of columns.In line 11, we print the pattern, and we have printed
j+1
, which results in iteration from 1 [since j + 1] to length ofi
in each row.i
keeps increasing with increasing rows, and so the numbers keep increasing as the line number increases.In line 14, we use
print[]
to move to the next line.
CONTRIBUTOR
Vinisha Maheshwari