{"id":20072,"date":"2021-09-06T13:36:36","date_gmt":"2021-09-06T08:06:36","guid":{"rendered":"https:\/\/python-programs.com\/?p=20072"},"modified":"2021-11-22T18:36:27","modified_gmt":"2021-11-22T13:06:27","slug":"python-program-to-find-sum-of-series-1-2-3-4-5-n","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-sum-of-series-1-2-3-4-5-n\/","title":{"rendered":"Python Program to Find Sum of Series 1-2+3-4+5…+N"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find Sum of Series 1+1\/3+1\/5+1\/7+…..1\/(N+2)<\/a><\/p>\n Given a number N and the task is to find the sum of the given series (1-2+3-4+5…+N)<\/strong>\u00a0for the given number in Python.<\/p>\n 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 find the sum of the given series (1-2+3-4+5…+N)<\/strong> for the given number 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 Find Sum of Series 1+1\/3+1\/5+1\/7+…..1\/(N+2) Given a number N and the task is to find the sum of the given series (1-2+3-4+5…+N)\u00a0for the given number in Python. Examples: Example1: Input: Given Number = 20 Output: The total sum of the series till the given number …<\/p>\nGiven Number = 20<\/pre>\n
The total sum of the series till the given number { 20 } = -10<\/pre>\n
Given Number = 7<\/pre>\n
The total sum of the series till the given number { 7 } = 4<\/pre>\n
Program to Find Sum of Series 1-2+3-4+5…+N in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the number N as static input and store it in a variable.\r\ngvn_numb = 20\r\n# Take a variable say resltsum which gives the sum of the given series till N and\r\n# initialize its value to 0.\r\nresltsum = 0\r\n# Loop from 1 to the given number using the for loop.\r\nfor itr in range(1, gvn_numb+1):\r\n # Inside the loop, check if the value of iterator modulus 2 is equal to zero(even condition)\r\n # or not using the if conditional statement.\r\n if itr % 2 == 0:\r\n # If the statement is True, then subtract the iterator value from the above-declared\r\n # resltsum.\r\n # Store it in the same variable resltsum.\r\n resltsum -= itr\r\n else:\r\n # Else if the statement is False, then add the iterator value to the above-declared\r\n # resltsum.\r\n # Store it in the same variable resltsum.\r\n resltsum += itr\r\n# Print the resltsum value which is the result of the series till the given Number N.\r\nprint(\r\n \"The total sum of the series till the given number {\", gvn_numb, \"} = \", resltsum)\r\n<\/pre>\n
The total sum of the series till the given number { 20 } = -10<\/pre>\n
Method #2: Using For 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 say resltsum which gives the sum of the given series till N and\r\n# initialize its value to 0.\r\nresltsum = 0\r\n# Loop from 1 to the given number using the for loop.\r\nfor itr in range(1, gvn_numb+1):\r\n # Inside the loop, check if the value of iterator modulus 2 is equal to zero(even condition)\r\n # or not using the if conditional statement.\r\n if itr % 2 == 0:\r\n # If the statement is True, then subtract the iterator value from the above-declared\r\n # resltsum.\r\n # Store it in the same variable resltsum.\r\n resltsum -= itr\r\n else:\r\n # Else if the statement is False, then add the iterator value to the above-declared\r\n # resltsum.\r\n # Store it in the same variable resltsum.\r\n resltsum += itr\r\n# Print the resltsum value which is the result of the series till the given Number N.\r\nprint(\r\n \"The total sum of the series till the given number {\", gvn_numb, \"} = \", resltsum)\r\n<\/pre>\n
Enter some Random Number = 7\r\nThe total sum of the series till the given number { 7 } = 4<\/pre>\n
\n