Program to Print Series 1,2,8,16,32...n

Python Program to Print Series 1, 2, 4, 8, 16, 32…n

In the previous article, we have discussed Python Program to Swap Upper Diagonal Elements with Lower Diagonal Elements of Matrix.
Given a number N and the task is to print the series (1,2,4,8,16,32…N) till the given number N in Python.

Examples:

Example1:

Input:

Given Number = 200

Output:

The above series till the given number{ 200 } is : 
1 2 4 8 16 32 64 128

Explanation:

1, 1*2, 2*2, 4*2, 8*2, 16*2, 32*2, 64*2
we cannot have 128*2=256 because it exceeds the given limit number 200.

Example2:

Input:

Given Number = 50

Output:

The above series till the given number{ 50 } is :
1 2 4 8 16 32

Explanation:

1, 1*2, 2*2, 4*2, 8*2, 16*2
we cannot have 32*2= 64 because it exceeds the given limit number 50.

Program to Print Series 1,2,8,16,32…n in Python

Below are the ways to print the series (1,2,8,16,32…N) till the given number N in Python:

Method #1: Using While Loop (Static Input)

Approach:

  • Give the number N as static input and store it in a variable.
  • Take a variable to say itr and initialize its value to 1.
  • Loop until the above-declared variable itr value is less than or equal to the given number using the while loop.
  • If it is true then print the value of itr separated by spaces.
  • Multiply the itr value with 2 and store it in the same variable itr.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as static input and store it in a variable.
gvn_numb = 200
# Take a variable to say itr and initialize its value to 1.
itr = 1
# Loop until the above-declared variable itr value is less than or equal to the
# given number using the while loop.
print("The above series till the given number{", gvn_numb, "} is :")
while itr <= gvn_numb:
  # If it is true then print the value of itr separated by spaces.
    print(itr, end=" ")
    # Multiply the itr value with 2 and store it in the same variable itr.
    itr *= 2

Output:

The above series till the given number{ 200 } is :
1 2 4 8 16 32 64 128

Method #2: Using While loop (User Input)

Approach:

  • Give the number N as user input using the int(input()) function and store it in a variable.
  • Take a variable to say itr and initialize its value to 1.
  • Loop until the above-declared variable itr value is less than or equal to the given number using the while loop.
  • If it is true then print the value of itr separated by spaces.
  • Multiply the itr value with 2 and store it in the same variable itr.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as user input using the int(input()) function and store it in a variable.
gvn_numb = int(input("Enter some Random Number = "))
# Take a variable to say itr and initialize its value to 1.
itr = 1
# Loop until the above-declared variable itr value is less than or equal to the
# given number using the while loop.
print("The above series till the given number{", gvn_numb, "} is :")
while itr <= gvn_numb:
  # If it is true then print the value of itr separated by spaces.
    print(itr, end=" ")
    # Multiply the itr value with 2 and store it in the same variable itr.
    itr *= 2

Output:

Enter some Random Number = 50
The above series till the given number{ 50 } is :
1 2 4 8 16 32

Find a comprehensive collection of Examples of Python Programs ranging from simple ones to complex ones to guide you throughout your coding journey.