input n as number then return '0' & '1' as number of n rows and columns in python

input n as number then return ‘0’ & ‘1’ as number of n rows and columns in python (without using any inbuilt type of function or library)

If input n = 3

The output: 

100
010
001

If input n = 4

The output:

1000
0100
0010
0001

I tried myself below, but i failed to achieve.

n = 3

for i in range(1, n+1):
    for j in range(i, n+1):
        if i == j:
            print(1)
        else:
            print(0)
        break

Anyone who can solve, will be much appreciated.

>Solution :

Here’s a simple example that forms each line into a string and then prints it to the console:

n = 3
for i in range(n): 
    line = ""
    for j in range(n):
        if i == j:
            line += "1"
        else:
            line += "0"
    print(line)

Leave a Reply