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. You will get what is the 1, 2, 4, 8, 16, 32 series in python, Print 1 2 4 8 16 In Python, Geometric Sequence Program In Java 1 2, 4, 8, 16, Program To Display Geometric Sequence In Java, Implement A Program To Display The Geometric Sequence In Java, Program That Print 1 2 4 8 16 32 64 128 Java, 1 2 4 8 16 Series In Java, N In Python, 1 2 4 7 11 Series In Python, Print 1 2 4 8 16 In Python Using For Loop, Series Program In Python, Geometric Progression Program In Java, Print Series In Python etc.. in this article.
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.
- Python Program to Print Double the Number Pattern
- Python Program to Print the Pyramid of Horizontal Number Tables
- Python Program to Find Sum of Series 1^1/1+2^2/2+3^3/3…+n^n/n
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.
You may try these:
- Write A Program To Print The Series 1 2 4 8 16 Till N Terms In Java
- How To Print 1 2 3 4 5 In Python
- How To Print 1 2 4 8 16 In Java
- Write While Loop To Display The Sequence 1 2 4 8 16 32
- How To Print Series In Python
- How To Print 1 2 3 4 5 In Python
Related Posts: