Sunday, June 4, 2017

Reverse Pattern Printing - Numbers

Numbers Based on the input value of N, the program must print the pattern described below.

Input Format: 

First line will contain the value of N.

Output Format:

 N lines will contain the number pattern as described below with each value separated by a single space.

Boundary Conditions: 

1 <= N <= 50

 Example Input/Output : 

Input:
5

Output:
15 10 6 3 1
14 9 5 2
13 8 4
12 7
11

Solution:

n = int(input())

# calculate the total no of elements in upper diagonal
d = int((n * n) - (n * (n - 1)) / 2)  

for i in range(n):
    s = d - i
    for j in range(n, i, -1):
        print(s, end=" ")
        s -= j
    print('\r')

Share: