{"id":22242,"date":"2021-09-23T19:10:41","date_gmt":"2021-09-23T13:40:41","guid":{"rendered":"https:\/\/python-programs.com\/?p=22242"},"modified":"2021-11-22T18:35:41","modified_gmt":"2021-11-22T13:05:41","slug":"python-program-to-print-series-0-2-8-14-24-34-n","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-series-0-2-8-14-24-34-n\/","title":{"rendered":"Python Program to Print Series 0, 2, 8, 14, 24, 34 …N"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Print Series 1, 22, 333, 4444…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 ( 0,2,8,14,24,34 …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 Grab the opportunity and utilize the Python Program Code Examples<\/a> over here to prepare basic and advanced topics too with ease and clear all your doubts.<\/p>\n In the previous article, we have discussed Python Program to Print Series 1, 22, 333, 4444…n Given a number N and the task is to print the series ( 0,2,8,14,24,34 …N) till the given number N in Python. Examples: Example1: Input: Given Number = 5 Output: The above series till the given number{ 5 } …<\/p>\n
\nGiven a number N and the task is to print the series ( 0,2,8,14,24,34 …N)<\/strong> till the given number N in Python.<\/p>\nGiven Number = 5<\/pre>\n
The above series till the given number{ 5 } is :\r\n0 2 8 14 24<\/pre>\n
Given Number = 9<\/pre>\n
The above series till the given number{ 9 } is :\r\n0 2 8 14 24 34 48 62 80<\/pre>\n
Program to Print Series 0,2,8,14,24,34 …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 = 5\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\nprint(\"The above series till the given number{\", gvn_numb, \"} is :\")\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\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 itr raised to the power 2 using the pow()\r\n # function and subtract 2 from it.\r\n # Store it in the same variable previous_val.\r\n previous_val = pow(itr, 2) - 2\r\n # Print the value of the above previous_val separated by spaces.\r\n print(previous_val, end=\" \")\r\n else:\r\n # Else, If it is false calculate the value of itr raised to the power 2 using the pow()\r\n # function and subtract 1 from it.\r\n # Store it in the same variable previous_val.\r\n previous_val = pow(itr, 2) - 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{ 5 } is :\r\n0 2 8 14 24<\/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\nprint(\"The above series till the given number{\", gvn_numb, \"} is :\")\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\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 itr raised to the power 2 using the pow()\r\n # function and subtract 2 from it.\r\n # Store it in the same variable previous_val.\r\n previous_val = pow(itr, 2) - 2\r\n # Print the value of the above previous_val separated by spaces.\r\n print(previous_val, end=\" \")\r\n else:\r\n # Else, If it is false calculate the value of itr raised to the power 2 using the pow()\r\n # function and subtract 1 from it.\r\n # Store it in the same variable previous_val.\r\n previous_val = pow(itr, 2) - 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 = 9\r\nThe above series till the given number{ 9 } is :\r\n0 2 8 14 24 34 48 62 80<\/pre>\n
\n