{"id":22220,"date":"2021-09-23T19:08:56","date_gmt":"2021-09-23T13:38:56","guid":{"rendered":"https:\/\/python-programs.com\/?p=22220"},"modified":"2021-11-22T18:35:41","modified_gmt":"2021-11-22T13:05:41","slug":"python-program-to-print-series-1-2-4-8-16-32-n","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-series-1-2-4-8-16-32-n\/","title":{"rendered":"Python Program to Print Series 1, 2, 4, 8, 16, 32…n"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Swap Upper Diagonal Elements with Lower Diagonal Elements of Matrix.<\/a> Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Below are the ways to print the series (1,2,8,16,32…N) <\/strong>till the given number N in Python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Find a comprehensive collection of Examples of Python Programs<\/a> ranging from simple ones to complex ones to guide you throughout your coding journey.<\/p>\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{ …<\/p>\n
\nGiven a number N and the task is to print the series (1,2,4,8,16,32…N) <\/strong>till the given number N in Python.<\/p>\nGiven Number = 200<\/pre>\n
The above series till the given number{ 200 } is : \r\n1 2 4 8 16 32 64 128<\/pre>\n
1, 1*2, 2*2, 4*2, 8*2, 16*2, 32*2, 64*2\r\nwe cannot have 128*2=256 because it exceeds the given limit number 200.<\/pre>\n
Given Number = 50<\/pre>\n
The above series till the given number{ 50 } is :\r\n1 2 4 8 16 32<\/pre>\n
1, 1*2, 2*2, 4*2, 8*2, 16*2\r\nwe cannot have 32*2= 64 because it exceeds the given limit number 50.<\/pre>\n
Program to Print Series 1,2,8,16,32…n in Python<\/h2>\n
\n
Method #1: Using While Loop (Static Input)<\/h3>\n
\n
# Give the number N as static input and store it in a variable.\r\ngvn_numb = 200\r\n# Take a variable to say itr and initialize its value to 1.\r\nitr = 1\r\n# Loop until the above-declared variable itr value is less than or equal to the\r\n# given number using the while loop.\r\nprint(\"The above series till the given number{\", gvn_numb, \"} is :\")\r\nwhile itr <= gvn_numb:\r\n # If it is true then print the value of itr separated by spaces.\r\n print(itr, end=\" \")\r\n # Multiply the itr value with 2 and store it in the same variable itr.\r\n itr *= 2\r\n<\/pre>\n
The above series till the given number{ 200 } is :\r\n1 2 4 8 16 32 64 128<\/pre>\n
Method #2: Using While loop (User Input)<\/h3>\n
\n
# Give the number N as user input using the int(input()) function and store it in a variable.\r\ngvn_numb = int(input(\"Enter some Random Number = \"))\r\n# Take a variable to say itr and initialize its value to 1.\r\nitr = 1\r\n# Loop until the above-declared variable itr value is less than or equal to the\r\n# given number using the while loop.\r\nprint(\"The above series till the given number{\", gvn_numb, \"} is :\")\r\nwhile itr <= gvn_numb:\r\n # If it is true then print the value of itr separated by spaces.\r\n print(itr, end=\" \")\r\n # Multiply the itr value with 2 and store it in the same variable itr.\r\n itr *= 2\r\n<\/pre>\n
Enter some Random Number = 50\r\nThe above series till the given number{ 50 } is :\r\n1 2 4 8 16 32<\/pre>\n
\n