{"id":22560,"date":"2021-09-27T08:33:27","date_gmt":"2021-09-27T03:03:27","guid":{"rendered":"https:\/\/python-programs.com\/?p=22560"},"modified":"2021-11-22T18:35:40","modified_gmt":"2021-11-22T13:05:40","slug":"python-program-to-print-series-1-2-6-15-31-n","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-series-1-2-6-15-31-n\/","title":{"rendered":"Python Program to Print Series 1, -2, 6, -15, 31 … N"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Print Series 1, 3, 7, 15, 31 … 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, -2, 6, -15, 31 … 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 Dive into numerous Python Programming Language Examples<\/a> for practice and get the best out of the tutorial and learn python one step at a time.<\/p>\n In the previous article, we have discussed Python Program to Print Series 1, 3, 7, 15, 31 … N Given a number N and the task is to print the series (1, -2, 6, -15, 31 … N) till the given number N in Python. Examples: Example1: Input: Given Number (Limit) = 8 Output: The …<\/p>\n
\nGiven a number N and the task is to print the series (1, -2, 6, -15, 31 … N)<\/strong> till the given number N in Python.<\/p>\nGiven Number (Limit) = 8<\/pre>\n
The above series till the given number{ 8 } is :\r\n1 -2 6 -15 31 -56 92 -141<\/pre>\n
Given Number (Limit) = 11<\/pre>\n
The above series till the given number{ 11 } is :\r\n1 -2 6 -15 31 -56 92 -141 205 -286 386<\/pre>\n
Program to Print Series 1, -2, 6, -15, 31 … N in Python<\/h2>\n
\n
Method #1: Using While Loop (Static Input)<\/h3>\n
\n
# Give the number N (Limit) 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 1.\r\nprevious_val = 1\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, then print the value of above previous_val multiplied with -1\r\n # separated by spaces.\r\n print(-1*previous_val, end=\" \")\r\n else:\r\n # Else, Print the value of the above previous_val separated by spaces.\r\n print(previous_val, end=\" \")\r\n # Calculate the value of itr raised to power 2 using the pow() function and\r\n # add it to the previous_val.\r\n # Store it in the same variable previous_val.\r\n previous_val += pow(itr, 2)\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 -2 6 -15 31 -56 92 -141<\/pre>\n
Method #2: Using While loop (User Input)<\/h3>\n
\n
# Give the number N (Limit) 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 1.\r\nprevious_val = 1\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, then print the value of above previous_val multiplied with -1\r\n # separated by spaces.\r\n print(-1*previous_val, end=\" \")\r\n else:\r\n # Else, Print the value of the above previous_val separated by spaces.\r\n print(previous_val, end=\" \")\r\n # Calculate the value of itr raised to power 2 using the pow() function and\r\n # add it to the previous_val.\r\n # Store it in the same variable previous_val.\r\n previous_val += pow(itr, 2)\r\n # Increment the above itr value by 1.\r\n itr += 1\r\n<\/pre>\n
Enter some Random Number = 11\r\nThe above series till the given number{ 11 } is :\r\n1 -2 6 -15 31 -56 92 -141 205 -286 386<\/pre>\n
\n