Right angle triangle program in python assignment

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

https://drive.google.com/file/d/1gSyv9i4BdnUeFcZrVWRneMTROebbSYOL/view?usp=sharing

We need exact output when the code was run

Right Angled Triangle - 3

Given an integer number 

N as input. Write a program to print the right-angled triangular pattern of N rows as shown below.

Input

The first line of input is a positive integer.

Explanation

For example, if the given number is 

5, the output should be

______
|    /
|   /
|  /
| /
|/
Sample Input 1
5
Sample Output 1
______
|    /
|   /
|  /
| /
|/
Sample Input 2
7
Sample Output 2
________
|      /
|     /
|    /
|   /
|  /
| /
|/

N = int(input())
for i in range(N+1):
    print('_', end='')
print()
for i in range(N):
    print('|', end='')
    print(' ' * (N - i - 1), end='')
    print('/')

Learn more about our help with Assignments: Python

In this shot, we will discuss how to generate a hollow right-angled triangle with stars in Python.

We can print a plethora of patterns with Python. The basic and only prerequisite is a good understanding of how loops work in Python.

Here, we will use simple for loops to generate a hollow right-angled triangle with stars.

Description

A triangle is said to be right-angled if it has an angle equal to 90 degrees on its left side.

We will use 2 for loops:

  • Outer loop: To handle the number of rows.
  • Inner loop: To handle the number of columns.

# Number of rows
n = 5
# Loop through rows
for i in range(1, n+1):
    # Loop through columns
    for j in range(1, n+1):
        # Printing Pattern
        if (i == j) or (j == 1) or (i == n):
            print("*", end=" ")
        else:
            print(" ", end=" ")
    print()  

Explanation

  • In line 2, we take the input for the number of rows (i.e. the length of the triangle).

  • In line 4, we create a for loop to iterate through the number of rows.

  • In line 6, we create an inner nested for loop to iterate through the number of columns.

  • From lines 8 to 11, we use conditional statements to create the pattern. The end statement helps to stay on the same line.

  • In line 12, we use the print() statement to move to the next line.

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.

PythonServer Side ProgrammingProgramming




Right angle triangle program in python assignment

Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

Mohammad Nauman

More Detail

Right angle triangle program in python assignment

Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

MANAS DASGUPTA

More Detail

Right angle triangle program in python assignment

Practical Data Science using Python

22 Lectures 6 hours

MANAS DASGUPTA

More Detail

Suppose we have two sides of a right angled triangle, these sides are AB and BC. Consider the midpoint of hypotenuse AC is M. We have to find the angle between M and BC.

So, if the input is like ab = 6 bc = 4, then the output will be 56.309932474020215 because arc_tan of ab/bc is 0.9828 but in degrees it is 56.31.

To solve this, we will follow these steps −

  • ans := arc-tan(ab/bc)
  • return ans in degrees

Example

Let us see the following implementation to get better understanding

from math import atan, pi
def solve(ab, bc):
   def deg(rad):
      return 180/pi * rad

   ans = deg(atan(ab/bc))
   return ans

ab = 6
bc = 4
print(solve(ab, bc))

Input

6, 4

Output

45.0

Right angle triangle program in python assignment

Arnab Chakraborty

Updated on 12-Oct-2021 07:54:39

  • Related Questions & Answers
  • Area of Circumcircle of a Right Angled Triangle?
  • Find the dimensions of Right angled triangle in C++
  • Area of Incircle of a Right Angled Triangle in C Program?
  • Area of Circumcircle of a Right Angled Triangle in C Program?
  • Find other two sides and angles of a right angle triangle in C++
  • Find other two sides of a right angle triangle in C++
  • Program to find the mid-point of a line in C++
  • Find the hypotenuse of a right angled triangle with given two sides in C++
  • Find the Number of Possible Pairs of Hypotenuse and Area to Form Right Angled Triangle using C++
  • Biggest Reuleaux Triangle within a Square which is inscribed within a Right angle Triangle?
  • Check whether right angled triangle is valid or not for large sides in Python
  • Biggest Reuleaux Triangle within a Square which is inscribed within a Right angle Triangle in C?
  • Program to find angle between hour and minute hands of a clock in C++?
  • Program to find number of ways we can reach from top left point to bottom right point in Python
  • Maximum number of squares that can fit in a right angle isosceles triangle in C++

Previous Page Print Page Next Page  

Advertisements

How do you find the angle of a right triangle in Python?

all of the trig functions convert between an angle and the ratio of two sides of a triangle. cos, sin, and tan take an angle in radians as input and return the ratio; acos, asin, and atan take a ratio as input and return an angle in radians. You only convert the angles, never the ratios.

How do you find the right angle triangle in a program?

#include<stdio.h>.
int main().
{ float b, h, area;.
b = 5;.
h= 8 ;.
area = ( b * h)/2 ;.
printf("\n\n Area of Right Angle Triangle is : %f",area);.
return (0);.

How do you solve a triangle in Python?

Python program to find the area of a triangle.
# Three sides of the triangle is a, b and c:.
a = float(input('Enter first side: ')).
b = float(input('Enter second side: ')).
c = float(input('Enter third side: ')).
# calculate the semi-perimeter..
s = (a + b + c) / 2..
# calculate the area..
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5..