Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading