Given a number N denoting the number of rows. The task is to print the zigzag pattern with N rows as shown in the below examples.
Here's a Python program to print a pyramid pattern using numbers:
# Get the number of rows from the user num_rows = int(input("Enter the number of rows: ")) # Loop through each row of the pyramid for i in range(1, num_rows + 1): # Print spaces before the numbers to center the pyramid print(" " * (num_rows - i), end="") # Loop through each number in the row for j in range(1, i + 1): # Print the number followed by a space print(j, end=" ") # Move to the next line after printing the row print()
In this program, the user is prompted to enter the number of rows they want in the pyramid. Then, a loop is used to iterate through each row of the pyramid.
Before printing the numbers for each row, a certain number of spaces are printed to center the pyramid. The number of spaces printed is determined by the difference between the total number of rows and the current row number.
After printing the spaces, another loop is used to print the numbers for that row. The loop starts at 1 and goes up to the current row number. Each number is printed followed by a space.
Finally, a new line character is printed to move to the next row of the pyramid.
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.