Odd number pattern in python|Taking limit from user and printing odd numbers within the limit|Write a program to print the full pyramid of odd number pattern .
Introduction
In this blog We are going to see two programs on odd numbers in python.
1) Print all odd numbers within the limits using for loop.In other words we will print all odd numbers in a range.
2)Write a program to print the full pyramid of odd number pattern. The pattern is shown below:
1
333
55555
7777777
999999999
Code 1:-Taking input from user and printing odd numbers within the limit.
start = int(input("Enter the start of range:"))
end = int(input("Enter the end of range:"))
print("odd numbers are:")
# iteration
for number in range(start, end + 1):
# checking odd numbers
if number % 2 != 0:
print(number)
Output:-
Executed on Python IDLE |
Understanding its working:-
- Take input from user for start and end limits.
- Then Executing print statement for better representation.
- We have Used For loop to iterate over the given range.
- Using if statement, we have checked whether the number is odd or not.We have checked this with the help of modulus(%) operator.
- If the number is odd print the number
- For loop
2):-Write a program to print the full pyramid of odd number pattern . The pattern is shown below:
1
333
55555
7777777
999999999
Code :-
number = 1
row_num = int(input("Enter the no. of rows:"))
for j in range(row_num):
i=row_num-1
while i>j:
print(" ", end="")
i=i-1
for i in range(number):
print(number, end="")
number=number+2
print()
Output:-Executed on Python IDLE
- Declaring variable.(number=1)
- Taking input from the user for total number of rows.
- We have used WHILE loop for preceeding and leading white spaces.(while i>j:)
- Then We have used FOR loop to print the number.(for i in range(number):)
- Then We have incremented the number by 2.(Since next odd number arrives at a difference of 2)
- Then executed a print()to print other numbers in the next row.
- For loop
- While Loop
"Hope you found it useful"
Have anything in mind? Let me
know in the comment section👇