In this article you will learn a Python program to print the pyramid of numbers with N Rows. Given a interior number N as a input to the Python program your task is to print the number pyramid on standard output
Let's try to solve this using only three lines of code.
Write a Program That Takes a Number `N` as Input And Prints a Pyramid of Numbers With `N` Rows.
Let's take a variable and ask a user to enter an integer number using the input function call and then convert it into an integer using type conversion.
n = int(input("Enter a Integer Number : "))
Now using the for loop, iterate from 1 to n + 1 and get the index out of this range.
To print the pyramid you should use the print function call
First print spaces, number minus index times
Second you have to convert the index to string and printed index types
Once the for loop is completed you will have a pyramid printed on to the standard output.
for index in range(1, n + 1):
print(" " * (n - index) + (str(index) + " ") * index)
Complete Python Program
===============================
n = int(input("Enter a Integer Number : "))
for index in range(1, n + 1):
print(" " * (n - index) + (str(index) + " ") * index)
Output 1
=============
Enter a Integer Number : 9
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9
Output 2
======================
Enter a Integer Number : 5
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Conclusion
===============================
Execute the above Python program by providing the random integer values between 1 to 9, this will print a beautiful pyramid pattern of numbers.
Comment it down below if you have any queries related to the above Python program.
0 Comments