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:-

  1. Take input from user for start and end limits.
  2. Then Executing print statement for better representation.
  3. We have Used For loop to iterate over the given range.
  4. Using if statement, we have checked whether the number is odd or not.We have checked this with the help of modulus(%) operator.
  5. If the number is odd print the number
Loops Used:-

  • 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


Understanding its working(Stepwise):-

  1. Declaring variable.(number=1)
  2. Taking input from the user for total number of rows.
  3. We have used WHILE loop for preceeding and leading white spaces.(while i>j:)
  4. Then We have used FOR loop to print the number.(for i in range(number):)
  5. Then We have incremented the number by 2.(Since next odd number arrives at a difference of 2)
  6. Then executed a print()to print other numbers in the next row.

Loops Used:-

  • For loop
  • While Loop


"Hope you found it useful"

Have anything in mind? Let me 
know in the comment section👇


 




Next Post Previous Post
No Comment
Add Comment
comment url