{"id":19930,"date":"2021-09-02T15:22:46","date_gmt":"2021-09-02T09:52:46","guid":{"rendered":"https:\/\/python-programs.com\/?p=19930"},"modified":"2021-11-22T18:36:29","modified_gmt":"2021-11-22T13:06:29","slug":"python-program-to-find-sum-of-series-1-2-2-3-3-4-4-5-5-6-n-n-1","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-sum-of-series-1-2-2-3-3-4-4-5-5-6-n-n-1\/","title":{"rendered":"Python Program to Find Sum of Series 1\/2-2\/3+3\/4-4\/5+5\/6…+N\/N+1"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find Sum of Series (1+(1*2)+(1*2*3)+…till N)<\/a><\/p>\n Given a number N and the task is to find the sum of the given series (1\/2-2\/3+3\/4-4\/5+5\/6…+N\/N+1)<\/strong> for 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-2\/3+3\/4-4\/5+5\/6…+N\/N+1)<\/strong>\u00a0for 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 Practice Python Program Examples<\/a> to master coding skills and learn the fundamental concepts in the dynamic programming language Python.<\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" In the previous article, we have discussed Python Program to Find Sum of Series (1+(1*2)+(1*2*3)+…till N) Given a number N and the task is to find the sum of the given series (1\/2-2\/3+3\/4-4\/5+5\/6…+N\/N+1) for the given number in Python. Examples: Example1: Input: Given Number = 17 Output: The total sum of the series till the …<\/p>\nGiven Number = 17<\/pre>\n
The total sum of the series till the given number { 17 } = 0.6661398242280596<\/pre>\n
Given Number = 6<\/pre>\n
The total sum of the series till the given number { 6 } = -0.2404761904761904<\/pre>\n
Program to Find Sum of Series 1\/2-2\/3+3\/4-4\/5+5\/6…+N\/N+1 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 = 17\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.0 (floating number).\r\nresltsum = 0.0\r\n# Take another variable say k and initialize its value with 1.\r\nk = 1\r\n# Loop until the value of k is less than or equal to the given number using the while loop.\r\nwhile(k <= gvn_numb):\r\n # Check if the value k modulus 2 is even or not using the if conditional statement.\r\n if(k % 2 == 0):\r\n # If the statement is True, then subtract\u00a0k\/(k+1) from the above-declared resltsum.\r\n # Store it in the same variable resltsum.\r\n resltsum -= k\/(k+1)\r\n else:\r\n # Else if the statement is False, then add k\/(k+1) to the above-declared resltsum.\r\n # Store it in the same variable resltsum.\r\n resltsum += k\/(k+1)\r\n # Increment the value of k by 1 and store it in the same variable k.\r\n k += 1\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 { 17 } = 0.6661398242280596<\/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 say resltsum which gives the sum of the given series till N and\r\n# initialize its value to 0.0 (floating number).\r\nresltsum = 0.0\r\n# Take another variable say k and initialize its value with 1.\r\nk = 1\r\n# Loop until the value of k is less than or equal to the given number using the while loop.\r\nwhile(k <= gvn_numb):\r\n # Check if the value k modulus 2 is even or not using the if conditional statement.\r\n if(k % 2 == 0):\r\n # If the statement is True, then subtract\u00a0k\/(k+1) from the above-declared resltsum.\r\n # Store it in the same variable resltsum.\r\n resltsum -= k\/(k+1)\r\n else:\r\n # Else if the statement is False, then add k\/(k+1) to the above-declared resltsum.\r\n # Store it in the same variable resltsum.\r\n resltsum += k\/(k+1)\r\n # Increment the value of k by 1 and store it in the same variable k.\r\n k += 1\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 = 6\r\nThe total sum of the series till the given number { 6 } = -0.2404761904761904<\/pre>\n
\n