Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

I just want to print the following binary pattern, for the given number of rows.

Binary pattern for N=4

1111

000

11

0

Binary pattern for N=5

11111

0000

111

00

1

1 Answer

0 votes
by (26.4k points)

Try the following code:

n = int(input())

ones = True

while n > 0:

  out = ["1"]*n if ones else ["0"]*n

  ones = not ones

  n -= 1

  print("".join(out))

Want to learn python to become expert in the concepts of python? Join python training course and get certified

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
4 answers
0 votes
5 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...