Advertisements
Advertisements
Question
Write a program to print the following pattern:
1 2 1 2 3 2 1 2 3 4 3 2 1 2 3 4 5 4 3 2 1 2 3 4 5 |
Solution
n = 5
for i in range (1, n + 1):
#This will print the space before the number
space = (n - i) * " "
print(space, end = " ")
#This for loop will print the decreasing numbers
for k in range(i, 1, -1):
print(k, end = " ")
#This for loop will print the increasing numbers
for j in range(1, i + 1):
print(j, end = " ")
#Print a new line after the space and numbers are printed
print()
APPEARS IN
RELATED QUESTIONS
What is the purpose of the range() function? Give one example.
Find the output of the following program segment:
for i in range(20,30,2):
print(i)
Find the output of the following program segment:
country = 'INDIA'
for i in country:
print (i)
Find the output of the following program segment:
for x in range(1,4):
for y in range(2,5):
if x * y > 10:
break
print (x * y)
Write a function to print the table of a given number. The number has to be entered by the user.
Write a program that prints the minimum and maximum of five numbers entered by the user.
Write a program to generate the sequence: –5, 10, –15, 20, –25….. up to n, where n is an integer input by the user.
Write a program to find the sum of 1 + 1/8 + 1/27......1/n3, where n is the number input by the user.
Write a program to print the following pattern:
1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 |
Write a program to print the following pattern:
* * * * * * * * |