Right Angled Triangle - 3
This Program name is Right Angled Triangle - 3. Write a Python program to Right Angled Triangle - 3, it has two test cases
The below link contains Leap Year question, explanation and test cases
//drive.google.com/file/d/1gSyv9i4BdnUeFcZrVWRneMTROebbSYOL/view?usp=sharing
We need exact output when the code was run
Python Basic - 1: Exercise-16 with Solution
Write a Python program to get the third side of right angled triangle from two given sides.
Note: Use bitwise operations to add two numbers.
Pictorial Presentation:
Sample Solution:
Python Code:
def pythagoras[opposite_side,adjacent_side,hypotenuse]:
if opposite_side == str["x"]:
return ["Opposite = " + str[[[hypotenuse**2] - [adjacent_side**2]]**0.5]]
elif adjacent_side == str["x"]:
return ["Adjacent = " + str[[[hypotenuse**2] - [opposite_side**2]]**0.5]]
elif hypotenuse == str["x"]:
return ["Hypotenuse = " + str[[[opposite_side**2] + [adjacent_side**2]]**0.5]]
else:
return "You know the answer!"
print[pythagoras[3,4,'x']]
print[pythagoras[3,'x',5]]
print[pythagoras['x',4,5]]
print[pythagoras[3,4,5]]
Sample Output:
Hypotenuse = 5.0 Adjacent = 4.0 Opposite = 3.0 You know the answer!
Flowchart:
Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Python Code Editor :
Have another way to solve this solution? Contribute your code [and comments] through Disqus.
Previous: Write a Python program to check the priority of the
four operators [+, -, *, /].
Next: Write a Python program to get all strobogrammatic numbers that are of length n.
Python: Tips of the Day
You cannot use a number at the beginning of the name of a variable
four_letters = �abcd� # this works 4_letters = �abcd� # this doesn�t work
Ref: //bit.ly/3A1PCO4
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.
In this shot, we will discuss how to generate a right-angled triangle using stars in Python.
We can print a plethora of patterns using python. The basic and only prerequisite for this 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.
Description
A right-angled triangle must have one angle equal to 90 degrees.
To execute
this using Python programming, we will use two for
loops:
- An outer loop: To handle the number of rows.
- An inner loop: To handle the number of columns.
Code
Let us see 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["*", end=' '] # Next Line print[]
Explanation
In line 2, we take the input for the number of rows [i.e. the length of the triangle].
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 [
*
]. Any other character could be printed by being in the print statement. The end statement helps us to be on the same line till the loop finishes.In line 14, we use
print[]
, to move to the next line.