Sure, here's a Python program that prints an even-odd number pyramid:
n = int(input("Enter the number of rows: ")) for i in range(1, n+1): for j in range(1, i+1): if (i+j) % 2 == 0: print("1", end=" ") else: print("0", end=" ") print()
In this program, the user is prompted to enter the number of rows they want in the pyramid. The outer for
loop iterates through each row, and the inner for
loop iterates through each column in that row. The condition (i+j) % 2 == 0
checks whether the sum of the row and column indices is even, in which case the program prints a 1, and otherwise it prints a 0.
Here's an example output of the program when the user enters 5 as the number of rows:
Enter the number of rows: 5 1 0 1 1 0 1 0 1 0 1 1 0 1 0 1
As you can see, the program prints a pyramid of alternating 0s and 1s, with each row having one more number than the previous row. The program can be modified to print a pyramid of any other alternating pattern simply by changing the values in the if-else
statement inside the inner for
loop.
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.