Number triangle in python assignment expert

Answer to Question #224843 in Python for hari

NUMBERED TRIANGLE
you are given an integer N. print N rows starting from 1 in the triangle order as shown in the explanation.
INPUT
the input contains an integer N .
OUTPUT
the output shoudl have N lines .
Each of the N lines should have space -seperated integers as per the traingle order.
EXPLANATION
given N = 5
the triangle order for the given N is
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15

n = int[input[]]
for i in range[n]:
    print[*[i + 1 + int[[n - [j + 1] / 2] * j] for j in range[i + 1]]]

Learn more about our help with Assignments: Python

Given a number N, write a program to print a triangular pattern of N lines with numbers as shown below.
Input

The input will be a single line containing a positive integer [N].
Output

The output should be N rows with numbers.
Note: There is no space between the numbers.
Explanation

For example, if the given number of rows is 4,
your code should print the following pattern
1
121
12321
1234321

Sample Input 1
4
Sample Output 1
1
121
12321
1234321

Sample Input 2
9
Sample Output 2
1
121
12321
1234321
123454321

1234567654321
123456787654321
12345678987654321


# Reading a value from user
n = int[input[]]

# Looping for n times
for i in range[1,n+1]:
    # Looping from 1 to i inclusive
    for j in range[1,i+1]:
        # Printing value of j
        print[j,end=""]
    # Looping from i-1 to 1 inclusive
    for j in range[i - 1, 0, -1]:
        # Printing value of j
        print[j, end=""]
    # Printing new line
    print[]

Screenshot:

Learn more about our help with Assignments: Python

Last update on August 19 2022 21:51:43 [UTC/GMT +8 hours]

Python Conditional: Exercise - 36 with Solution

Write a Python program to check a triangle is equilateral, isosceles or scalene.
Note :
An equilateral triangle is a triangle in which all three sides are equal.
A scalene triangle is a triangle that has three unequal sides.
An isosceles triangle is a triangle with [at least] two equal sides.

Pictorial Presentation:

Sample Solution:

Python Code:

print["Input lengths of the triangle sides: "]
x = int[input["x: "]]
y = int[input["y: "]]
z = int[input["z: "]]

if x == y == z:
	print["Equilateral triangle"]
elif x==y or y==z or z==x:
	print["isosceles triangle"]
else:
	print["Scalene triangle"]

Sample Output:

x: 6                                                                                                          
y: 8                                                                                                          
z: 12                                                                                                         
Scalene triangle  

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 a string represent an integer or not?
Next: Write a Python program that reads two integers representing a month and day and prints the season for that month and day.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Python: Tips of the Day

Difference In Sets:

To retrieve the difference between two sets:

a = {1,2,3}
b = {3,4,5}w
c = a.difference[b]

  • Exercises: Weekly Top 16 Most Popular Topics
  • SQL Exercises, Practice, Solution - JOINS
  • SQL Exercises, Practice, Solution - SUBQUERIES
  • JavaScript basic - Exercises, Practice, Solution
  • Java Array: Exercises, Practice, Solution
  • C Programming Exercises, Practice, Solution : Conditional Statement
  • HR Database - SORT FILTER: Exercises, Practice, Solution
  • C Programming Exercises, Practice, Solution : String
  • Python Data Types: Dictionary - Exercises, Practice, Solution
  • Python Programming Puzzles - Exercises, Practice, Solution
  • C++ Array: Exercises, Practice, Solution
  • JavaScript conditional statements and loops - Exercises, Practice, Solution
  • C# Sharp Basic Algorithm: Exercises, Practice, Solution
  • Python Lambda - Exercises, Practice, Solution
  • Python Pandas DataFrame: Exercises, Practice, Solution
  • Conversion Tools
  • JavaScript: HTML Form Validation

Chủ Đề