{"id":9229,"date":"2021-10-01T11:00:55","date_gmt":"2021-10-01T05:30:55","guid":{"rendered":"https:\/\/python-programs.com\/?p=9229"},"modified":"2021-11-22T18:33:26","modified_gmt":"2021-11-22T13:03:26","slug":"python-program-to-find-the-sum-of-the-series-1-1-2-1-3-1-n","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-sum-of-the-series-1-1-2-1-3-1-n\/","title":{"rendered":"Python Program to Find the Sum of the Series: 1 + 1\/2 + 1\/3 + \u2026.. + 1\/N"},"content":{"rendered":"
Given a number , the task is to find the sum of series 1 + 1 \/ 2 + …… + 1 \/number<\/strong> in Python.<\/p>\n The inverse of a series is considered to be in Harmonic Progression if it follows the rule of an A.P, i.e. Arithmetic Progression. In general, the terms in a harmonic progression can be written as 1\/a, 1\/(a + d), 1\/(a + 2d), 1\/(a + 3d),…. 1\/(a + nd). 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 There are several ways to find the sum of the given series some of them are:<\/p>\n The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples<\/a> as it includes basic to advanced levels of concepts.<\/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 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 Related Programs<\/strong>:<\/p>\n Given a number , the task is to find the sum of series 1 + 1 \/ 2 + …… + 1 \/number in Python. The inverse of a series is considered to be in Harmonic Progression if it follows the rule of an A.P, i.e. Arithmetic Progression. In general, the terms in a harmonic …<\/p>\n
\nAs the Nth term of AP is (a + (n \u2013 1)d).
\nAs a result, the Nth term of harmonic progression is the reciprocal of the Nth term of AP, which is: 1\/(a + (n \u2013 1)d), where “a” is the first term of AP and “d” is the common difference.<\/p>\ntotal number of terms = 17<\/pre>\n
The total sum of the series 3.44<\/pre>\n
Enter the total numbers of terms =26<\/pre>\n
The total sum of the series 3.85<\/pre>\n
Program to Find the Sum of the Series: 1 + 1\/2 + 1\/3 + \u2026.. + 1\/N in Python<\/h2>\n
\n
Method #1:Using for loop (Static input)<\/h3>\n
\n
# Give the number of terms as static input.\r\nnumb = 17\r\n# Set a totalsum variable which calculates the total sum and initialize it to 0.\r\ntotalsum = 0\r\n# Find the total of the series using a for loop ranging from 1 to the number.\r\nfor value in range(1, numb+1):\r\n # calculating the total sum\r\n totalsum = totalsum+(1\/value)\r\n# Print the totalsum of the series, rounded to two decimal places.\r\nprint(\"The total sum of the series \", round(totalsum, 2))\r\n<\/pre>\n
The total sum of the series 3.44<\/pre>\n
Method #2:Using for loop (User input)<\/h3>\n
\n
# Scan\u00a0the\u00a0total\u00a0number\u00a0of\u00a0terms\u00a0as\u00a0user\u00a0input\u00a0by\u00a0using\u00a0int(input())\u00a0function.\r\nnumb = int(input(\"Enter the total numbers of terms =\"))\r\n# Set a totalsum variable which calculates the total sum and initialize it to 0.\r\ntotalsum = 0\r\n# Find the total of the series using a for loop ranging from 1 to the number.\r\nfor value in range(1, numb+1):\r\n # calculating the total sum\r\n totalsum = totalsum+(1\/value)\r\n# Print the totalsum of the series, rounded to two decimal places.\r\nprint(\"The total sum of the series \", round(totalsum, 2))\r\n<\/pre>\n
Enter the total numbers of terms =26\r\nThe total sum of the series 3.85<\/pre>\n
Method #3:Using while loop (User input)<\/h3>\n
\n
# Scan\u00a0the\u00a0total\u00a0number\u00a0of\u00a0terms\u00a0as\u00a0user\u00a0input\u00a0by\u00a0using\u00a0int(input())\u00a0function.\r\nnumb = int(input(\"Enter the total numbers of terms =\"))\r\n# Take a variable say value and initialize it to 1.\r\nvalue = 1\r\n# Set a totalsum variable which calculates the total sum and initialize it to 0.\r\ntotalsum = 0\r\n# Using while loop calculate the total sum till value greater than given number\r\nwhile(value <= numb):\r\n # calculating the total sum\r\n totalsum = totalsum+(1\/value)\r\n # Increment the value by 1.\r\n value = value+1\r\n# Print the totalsum of the series, rounded to two decimal places.\r\nprint(\"The total sum of the series \", round(totalsum, 2))\r\n<\/pre>\n
Enter the total numbers of terms =26\r\nThe total sum of the series 3.85<\/pre>\n
Method #4:Using While loop( Static Input)<\/h3>\n
\n
# Give the number of terms as static input.\r\nnumb = 26\r\n# Take a variable say value and initialize it to 1.\r\nvalue = 1\r\n# Set a totalsum variable which calculates the total sum and initialize it to 0.\r\ntotalsum = 0\r\n# Using while loop calculate the total sum till value greater than given number\r\nwhile(value <= numb):\r\n # calculating the total sum\r\n totalsum = totalsum+(1\/value)\r\n # Increment the value by 1.\r\n value = value+1\r\n# Print the totalsum of the series, rounded to two decimal places.\r\nprint(\"The total sum of the series \", round(totalsum, 2))\r\n<\/pre>\n
The total sum of the series 3.85<\/pre>\n
\n