{"id":22245,"date":"2021-09-23T19:11:08","date_gmt":"2021-09-23T13:41:08","guid":{"rendered":"https:\/\/python-programs.com\/?p=22245"},"modified":"2021-11-22T18:35:41","modified_gmt":"2021-11-22T13:05:41","slug":"python-program-to-print-series-1-9-17-33-49-73-97-n","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-series-1-9-17-33-49-73-97-n\/","title":{"rendered":"Python Program to Print Series 1 9 17 33 49 73 97 …N"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Print Series 0, 2, 8, 14, 24, 34 …N<\/a> Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to print the series (1 9 17 33 49 73 97 …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 If you are learning Python then the Python Programming Example<\/a> is for you and gives you a thorough description of concepts for beginners, experienced programmers.<\/p>\n In the previous article, we have discussed Python Program to Print Series 0, 2, 8, 14, 24, 34 …N Given a number N and the task is to print the series (1 9 17 33 49 73 97 …N)\u00a0till the given number N in Python. Examples: Example1: Input: Given Number = 8 Output: The above …<\/p>\n
\nGiven a number N and the task is to print the series (1 9 17 33 49 73 97 …N)<\/strong>\u00a0till the given number N in Python.<\/p>\nGiven Number = 8<\/pre>\n
The above series till the given number{ 8 } is :\r\n1 9 17 33 49 73 97 129<\/pre>\n
Given Number = 4<\/pre>\n
The above series till the given number{ 4 } is :\r\n1 9 17 33<\/pre>\n
Program to Print Series 1 9 17 33 49 73 97 …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 = 8\r\n# Take a variable to say itr and initialize its value to 1.\r\nitr = 1\r\n# Take another variable say previous_val and initialize its value to 0.\r\nprevious_val = 0\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 # Inside the loop, check if the above itr value is even or not using the if\r\n # conditional statement.\r\n if(itr % 2 == 0):\r\n # If it is true, calculate the value of 2 multiplied with the above itr value raised\r\n # to the power 2 using the pow() function.\u00a0\r\n # Store it in a variable.\r\n k = 2*pow(itr, 2)\r\n # Add 1 to the above result and store it in the same variable\u00a0previous_val.\r\n previous_val = k+1\r\n # Print the value of the above previous_val separated by spaces.\r\n print(previous_val, end=\" \")\r\n else:\r\n # If it is false, calculate the value of 2 multiplied with the above itr value raised\r\n # to the power 2 using the pow() function.\u00a0\r\n # Store it in another variable.\r\n l = 2*pow(itr, 2)\r\n # Subtract 1 from the\u00a0above result and store it in the same variable\u00a0previous_val.\r\n previous_val = l - 1\r\n # Print the value of the above previous_val separated by spaces.\r\n print(previous_val, end=\" \")\r\n # Increment the above itr value by 1.\r\n itr += 1\r\n<\/pre>\n
The above series till the given number{ 8 } is :\r\n1 9 17 33 49 73 97 129<\/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\r\n# 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# Take another variable say previous_val and initialize its value to 0.\r\nprevious_val = 0\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 # Inside the loop, check if the above itr value is even or not using the if\r\n # conditional statement.\r\n if(itr % 2 == 0):\r\n # If it is true, calculate the value of 2 multiplied with the above itr value raised\r\n # to the power 2 using the pow() function.\u00a0\r\n # Store it in a variable.\r\n k = 2*pow(itr, 2)\r\n # Add 1 to the above result and store it in the same variable\u00a0previous_val.\r\n previous_val = k+1\r\n # Print the value of the above previous_val separated by spaces.\r\n print(previous_val, end=\" \")\r\n else:\r\n # If it is false, calculate the value of 2 multiplied with the above itr value raised\r\n # to the power 2 using the pow() function.\u00a0\r\n # Store it in another variable.\r\n l = 2*pow(itr, 2)\r\n # Subtract 1 from the\u00a0above result and store it in the same variable\u00a0previous_val.\r\n previous_val = l - 1\r\n # Print the value of the above previous_val separated by spaces.\r\n print(previous_val, end=\" \")\r\n # Increment the above itr value by 1.\r\n itr += 1\r\n<\/pre>\n
Enter some Random Number = 4\r\nThe above series till the given number{ 4 } is :\r\n1 9 17 33<\/pre>\n
\n