Sunday, August 6, 2017

Triangle Pattern

Given an odd integer N, print a triangle with * as mentioned in the below examples.

Input Format:
The first line contains N.

Output Format:
N/2 + 1 lines containing the triangle pattern as shown in the example input/output.

Boundary Conditions:
1 <= N <= 999

Example Input/Output 1:
Input:
5

Output:
!!*!!
!***!
*****

Example Input/Output 2:
Input:
9

Output:
!!!!*!!!!
!!!***!!!
!!*****!!
!*******!
*********


Solution-1:
n = int(input())
print('\n'.join(('*'*i).center(n,'!')for i in range(1,n+1,2)))

Solution-2:
n = int(input())
for i in range(n//2+1):
    for j in range(n):
        if j >= n//2-i and j <= n//2+i:
            print('*',end='')
        else:
            print('!',end='')
    print()
Share: