Search code examples
loopspseudocode

Write pseudocode of a program that prints such a pattern


Analyze output pattern and write algorithm of a program that prints such a pattern.

Input 4
Pattern:

55555
4444
333
22
1

Input 3
Pattern:

333
22
1

Process (what I have come up with)

n  = input (“Enter a positive integer”)
r= 0
while r < n 
    c = (n – r) + 1
    while c > 0
        s = n – r
        print s 
        c = c – 1
    end
    r = r + 1
    n = n – 1
    print end l
end

Problem: I have used r for rows, and c for columns. The problem rises in c = (n – r) + 1 for first row. It makes the first row n+1, works for following rows. On dry run i get

Input 3
Pattern:
444
22
1


Solution

  • This should work:

    n = input (“Enter a positive integer”)
    while n > 0 
        c = n
        while c > 0
            print n
            c = c – 1
        end
        n = n - 1
        print end l
    end
    

    Be careful about what meaning you give to your variables and therefore, how you treat them consitently ;)