;

Python Program to Print Binary Number 0 and 1 in an alternative Columns


Tutorialsrack 14/05/2020 Python

In this python program, we will learn how to print binary numbers 0 and 1 in alternate columns and also print the reversed output 1 and 0 in alternate columns.

Here is the source code of the program to print binary numbers 0 and 1 in alternate columns.

Program 1: Python Program to Print Binary Number 0 and 1 in an Alternative Columns Using For Loop

Python Program to Print Binary Number 0 and 1 in an Alternative Columns Using For Loop
# Python Program to Print Binary Number 0 and 1 in an Alternative Columns
# Using For Loop

# Take the Input From the User
rows = int(input("Enter the total Number of Rows: "))
columns = int(input("Enter the total Number of Columns: "))

# Print the Output
print("Print Binary Number Pattern 1 and 0 in an alternative Columns") 
 
for i in range(1, rows + 1):
    for j in range(1, columns + 1):
        if(j % 2 == 0):          
            print('0', end = '  ')
        else:
            print('1', end = '  ')
    print()
Syntax - SELECT Statement

Enter the total Number of Rows: 5

Enter the total Number of Columns: 5

Print Binary Number Pattern 1 and 0 in an alternative Columns

1  0  1  0  1  

1  0  1  0  1  

1  0  1  0  1  

1  0  1  0  1  

1  0  1  0  1 

Program 2: Python Program to Print Binary Number 0 and 1 in Alternative Columns Using While Loop

Python Program to Print Binary Number 0 and 1 in an Alternative Columns Using While Loop
# Python Program to Print Binary Number 0 and 1 in an Alternative Columns
# Using While Loop

# Take the Input From the User
rows = int(input("Enter the total Number of Rows: "))
columns = int(input("Enter the total Number of Columns: "))

# Print the Output
print("Print Binary Number Pattern 1 and 0 in an alternative Columns") 
i = 1
while(i <= rows):
    j = 1
    while(j <= columns):
        if(j % 2 != 0):          
            print('1', end = '  ')
        else:
            print('0', end = '  ')
        j = j + 1
    i = i + 1
    print()
Output

Enter the total Number of Rows: 4

Enter the total Number of Columns: 4

Print Binary Number Pattern 1 and 0 in an alternative Columns

1  0  1  0  

1  0  1  0  

1  0  1  0  

1  0  1  0

Program 3: Python Program to Print Binary Number 0 and 1 in an Alternative Columns Without Using if...else

Python Program to Print Binary Number 0 and 1 in an Alternative Columns Without Using if...else
# Python Program to Print Binary Number 0 and 1 in an Alternative Columns
# Without Using if...else

# Take the Input From the User
rows = int(input("Enter the total Number of Rows: "))
columns = int(input("Enter the total Number of Columns: "))

# Print The Output
print("Print Binary Number Pattern 1 and 0 in an alternative Columns") 
 
for i in range(1, rows + 1):
    for j in range(1, columns + 1):
        print('%d' %(j % 2), end = '  ')
    print()
Output

Enter the total Number of Rows: 6

Enter the total Number of Columns: 6

Print Binary Number Pattern 1 and 0 in an alternative Columns

1  0  1  0  1  0  

1  0  1  0  1  0  

1  0  1  0  1  0  

1  0  1  0  1  0  

1  0  1  0  1  0  

1  0  1  0  1  0

Program 4: If you want to Print first 0 and second 1 then replace the values

If you want to Print first 0 and second 1 then replace the values
# Python Program to Print Binary Number 0 and 1 in an alternative Columns

# Take the Input From the User
rows = int(input("Enter the total Number of Rows: "))
columns = int(input("Enter the total Number of Columns: "))

# Print the Output
print("Print Binary Number Pattern 1 and 0 in an alternative Columns")
 
for i in range(1, rows + 1):
    for j in range(1, columns + 1):
        if(j % 2 == 0):          
            print('1', end = '  ')
        else:
            print('0', end = '  ')
    print()
Output

Enter the total Number of Rows: 5

Enter the total Number of Columns: 5

Print Binary Number Pattern 1 and 0 in an alternative Columns

0  1  0  1  0  

0  1  0  1  0  

0  1  0  1  0  

0  1  0  1  0  

0  1  0  1  0


Related Posts



Comments

Recent Posts
Tags