{"id":19936,"date":"2021-09-02T15:24:33","date_gmt":"2021-09-02T09:54:33","guid":{"rendered":"https:\/\/python-programs.com\/?p=19936"},"modified":"2021-11-22T18:36:29","modified_gmt":"2021-11-22T13:06:29","slug":"python-program-to-find-the-sum-of-series-1-x-x2-2-xn-n","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-sum-of-series-1-x-x2-2-xn-n\/","title":{"rendered":"Python Program to Find the Sum of Series 1+X+X^2\/2…+X^N\/N"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find the Sum of Series 1+X+X^3…+X^N<\/a><\/p>\n Given the numbers, N and X and the task is to find the sum of the given series (1+X+X^2\/2…+X^N\/N)<\/strong>\u00a0for the given numbers 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+X+X^2\/2…+X^N\/N) <\/strong>for the given numbers 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 If you are learning Python then the Python Programming Example<\/a> is for you and gives you a thorough description of concepts for beginners, experienced programmers.<\/p>\n In the previous article, we have discussed Python Program to Find the Sum of Series 1+X+X^3…+X^N Given the numbers, N and X and the task is to find the sum of the given series (1+X+X^2\/2…+X^N\/N)\u00a0for the given numbers in Python. Examples: Example1: Input: Given Number = 7 Given X value = 6 Output: The total …<\/p>\nGiven Number = 7\r\nGiven X value = 6<\/pre>\n
The total sum of the series till the given number N and x value { 7 6 } = 49743.05714285714<\/pre>\n
Given Number = 4\r\nGiven X value = 3<\/pre>\n
The total sum of the series till the given number N and x value { 4 3 } = 37.75<\/pre>\n
Program to Find the Sum of Series 1+X+X^2\/2…+X^N\/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 = 7\r\n# Give the value of X as static input and store it in another variable.\r\ngvn_x_val = 6\r\n# Take a variable say resltsum which gives the sum of the given series till N and\r\n# initialize its value to 1.0 (to get in float format).\r\nresltsum = 1.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 # Inside the loop, calculate the value of X raised to the power k divided by k using the\r\n # pow() function and store it in a variable say p.\r\n p = pow(gvn_x_val, k)\/k\r\n # Add the above value of p to the\u00a0resltsum and store it in the same variable resltsum.\r\n resltsum += p\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 N and x value {\", gvn_numb, gvn_x_val, \"} = \", resltsum)\r\n<\/pre>\n
The total sum of the series till the given number N and x value { 7 6 } = 49743.05714285714<\/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# Give the value of X as user input using the int(input()) function and\r\n# store it in another variable.\r\ngvn_x_val = 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 1.0 (to get in float format).\r\nresltsum = 1.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 # Inside the loop, calculate the value of X raised to the power k divided by k using the\r\n # pow() function and store it in a variable say p.\r\n p = pow(gvn_x_val, k)\/k\r\n # Add the above value of p to the\u00a0resltsum and store it in the same variable resltsum.\r\n resltsum += p\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 N and x value {\", gvn_numb, gvn_x_val, \"} = \", resltsum)\r\n<\/pre>\n
Enter some Random Number = 4\r\nEnter some Random Number = 3\r\nThe total sum of the series till the given number N and x value { 4 3 } = 37.75<\/pre>\n
\n