Python program to print zeros and stars pattern

Python Pattern Program
Python Pattern Program

Problem Statement

Print the star zero pattern for a given number of rows N

Input Format

Integer N

Output Format

The star zero pattern in N lines

Examples

Sample Input 1

3

Sample Output 1

*00*00*
0*0*0*0
00***00

Sample Input 2

5

Sample Output 2

*0000*0000*
0*000*000*0
00*00*00*00
000*0*0*000
0000***0000

Solution Approach

Read N, for each row print left zeros, then star, then middle zeros, second star, middle zeros again, third star, then right zeros, continue till all rows print

Python Code

N = int(input())

i = 1
while i <= N:
    # Left zeros
    j = 1
    while j <= i - 1:
        print(0, end="")
        j += 1

    # First star
    print("*", end="")

    # Middle zeros
    j = N - i
    while j > 0:
        print(0, end="")
        j -= 1

    # Second star
    print("*", end="")

    # More middle zeros
    j = N - i
    while j > 0:
        print(0, end="")
        j -= 1

    # Third star
    print("*", end="")

    # Right zeros
    j = 1
    while j <= i - 1:
        print(0, end="")
        j += 1

    print()
    i += 1
Previous Post Next Post