Print 1 and 0 in Python Program in Alternate Rows

Posted by

Python Program to Print 1 and 0 in alternative rows

Python Program to Print 1 and 0 in alternative rows

Below is a simple Python program that prints 1 and 0 in alternative rows:


n = 5  # Number of rows to print

for i in range(n):
    if i % 2 == 0:
        print('1 ' * n)
    else:
        print('0 ' * n)

This program uses a for loop to iterate through the specified number of rows (in this case, 5). It then checks if the row number is even or odd using the modulo operator (%). If the row number is even, it prints ‘1’ repeated n times, and if the row number is odd, it prints ‘0’ repeated n times.

You can run this Python program in any Python interpreter to see the output.