Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n - GeeksforGeeks

12 Apr, 2021
This is a mathematical series program where a user must enter the number of terms up to which the sum of the series is to be found. Following this, we also need the value of x, which forms the base of the series.Examples :  
Input : base = 2, range = 5
Output : 18.07

Input : base = 1, range = 10
Output : 3.93
 
Method 1 (Simple) We just need to follow the series and put the values of the base at x and value range at n and get the sum. 
C++
C
Java
Python3
C#
PHP
Javascript
// C++ program to find sum of series// 1 + x/1 + x^2/2 + x^3/3 + ....+ x^n/n#include <math.h>#include <iostream>#include <boost/format.hpp>class gfg{public :double sum(int x, int n){    double i, total = 1.0;    for (i = 1; i <= n; i++)        total = total +                (pow(x, i) / i);    return total;}};// Driver codeint main(){    gfg g;    int x = 2;    int n = 5;    //std::cout<<g.sum(x,n);    std::cout << boost::format("%.2f") % g.sum(x,n);    return 0;}
// C program to find sum of series// 1 + x/1 + x^2/2 + x^3/3 + ....+ x^n/n#include <math.h>#include <stdio.h> double sum(int x, int n){    double i, total = 1.0;    for (i = 1; i <= n; i++)        total = total +                (pow(x, i) / i);    return total;} // Driver codeint main(){    int x = 2;    int n = 5;    printf("%.2f", sum(x, n));    return 0;}
// Java program to find sum of series// 1 + 1/x + x^2/2 + x^3/3 + ....+ x^n/nimport static java.lang.Math.pow; class GFG{     // Java code to print the// sum of the seriesstatic double sum(int x, int n){    double i, total = 1.0;    for (i = 1; i <= n; i++)        total = total +                (Math.pow(x, i) / i);     return total;} // Driver codepublic static void main(String[] args){    int x = 2;    int n = 5;    System.out.printf("%.2f", sum(x, n));}} // This code is contributed by// Smitha Dinesh Semwal
# Python3 code to find sum of series# 1 + x/1 + x^2/2 + x^3/3 + .. .+ x^n/n def SUM(x, n):    total = 1    for i in range(1, n + 1):        total = total + ((x**i)/i)    return total # Driver Codex = 2n = 5s = SUM(x, n)print(round(s, 2))
// C# program to find sum of series// 1 + 1/x + x^2/2 + x^3/3 + ....+ x^n/nusing System; class GFG{     // Java code to print the    // sum of the series    static float sum(int x, int n)    {        double i, total = 1.0;        for (i = 1; i <= n; i++)            total = total +                    (Math.Pow(x, i) / i);         return (float)total;    }     // Driver code    public static void Main()    {        int x = 2;        int n = 5;        Console.WriteLine(sum(x, n));    }} // This code is contributed by vt_m.
<?php// PHP program to find sum of series// 1 + x/1 + x^2/2 + x^3/3 + ....+ x^n/n // Code to print the sum// of the seriesfunction sum($x, $n){    $i; $total = 1.0;    for ($i = 1; $i <= $n; $i++)        $total = $total +                 (pow($x, $i) / $i);    return $total;} // Driver code$x = 2;$n = 5;echo(sum($x, $n)); // This code is contributed by Ajit.?>
<script>// JavaScript program to find sum of series// 1 + x/1 + x^2/2 + x^3/3 + ....+ x^n/nfunction sum(x, n){    let i, total = 1.0;    for (i = 1; i <= n; i++)        total = total +                (Math.pow(x, i) / i);    return total;} // Driver code    let g;    let x = 2;    let n = 5;    document.write(sum(x, n).toFixed(2)); // This code is contributed by Surbhi Tyagi.</script>
Output :  
18.07
Method 2 (Optimized) We can avoid use of pow() function and reuse the previously computed power.  
C++
C
Java
Python3
C#
PHP
Javascript
// C++ program to find sum of series// 1 + x^2/2 + x^3/3 + ....+ x^n/n#include <bits/stdc++.h>using namespace std; // C++ code to print the sum// of the seriesdouble sum(int x, int n){    double i, total = 1.0, multi = x;    for (i = 1; i <= n; i++)    {        total = total + multi / i;        multi = multi * x;    }    return total;} // Driver codeint main(){    int x = 2;    int n = 5;    cout << fixed << setprecision(2) << sum(x, n);    return 0;} // This code is contributed by shubhamsingh10
// C program to find sum of series// 1 + x^2/2 + x^3/3 + ....+ x^n/n#include <math.h>#include <stdio.h> // C code to print the sum// of the seriesdouble sum(int x, int n){    double i, total = 1.0, multi = x;    for (i = 1; i <= n; i++) {        total = total + multi / i;        multi = multi * x;    }    return total;} // Driver codeint main(){    int x = 2;    int n = 5;    printf("%.2f", sum(x, n));    return 0;}
// Java program to find sum of series// 1 + x^2/2 + x^3/3 + ....+ x^n/n class GFG{ // Java code to print the sum// of the given seriesstatic double sum(int x, int n){    double i, total = 1.0, multi = x;    for (i = 1; i <= n; i++)    {        total = total + multi / i;        multi = multi * x;    }    return total;} // Driver codepublic static void main(String[] args){    int x = 2;    int n = 5;    System.out.printf("%.2f", sum(x, n));}} // This code is contributed by// Smitha Dinesh Semwal
# Python 3 program to find sum of series# 1 + x^2/2 + x^3/3 + ....+ x^n/n # Python 3 code to print the# sum of the seriesdef sum(x, n):     total = 1.0    multi = x    for i in range(1, n+1):        total = total + multi / i        multi = multi * x         return total  # Driver codex = 2n = 5print(round(sum(x, n), 2)) # This code is contributed by# Smitha Dinesh Semwal
// C# program to find sum of series// 1 + x^2/2 + x^3/3 + ....+ x^n/nusing System; class GFG{     // Java code to print the sum    // of the given series    static float sum(int x, int n)    {        double i, total = 1.0, multi = x;        for (i = 1; i <= n; i++)        {            total = total + multi / i;            multi = multi * x;        }        return (float)total;    }     // Driver code    public static void Main()    {        int x = 2;        int n = 5;        Console.WriteLine(sum(x, n));    }} // This code is contributed by vt_m.
<?php// PHP program to find sum of series// 1 + x^2/2 + x^3/3 + ....+ x^n/n // code to print the sum// of the seriesfunction sum($x, $n){    $i; $total = 1.0; $multi = $x;    for ($i = 1; $i <= $n; $i++)    {        $total = $total + $multi / $i;        $multi = $multi * $x;    }    return $total;} // Driver code$x = 2;$n = 5;echo(sum($x, $n)); // This code is contributed by Ajit.?>
<script> // Javascript  program to find sum of series// 1 + x^2/2 + x^3/3 + ....+ x^n/n // JavaScript code to print the sum// of the seriesfunction sum(x, n){         let total = 1.0;    let multi = x;    for (let i = 1; i <= n; i++)    {        total = total + multi / i;        multi = multi * x;    }    return total;} // Driver codelet x = 2;let n = 5;document.write(sum(x, n).toFixed(2)); // This code is contributed by sravan kumar </script>
Output :  
18.07
 
jit_t
SoumikMondal
SHUBHAMSINGH10
surbhityagi15
sravankumar8128
series
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Modulo Operator (%) in C/C++ with Examples
Program for factorial of a number
Print all possible combinations of r elements in a given array of size n
The Knight's tour problem | Backtracking-1
Operators in C / C++
Find minimum number of coins that make a given value
Program to find sum of elements in a given array
Minimum number of jumps to reach end
Program to print prime numbers from 1 to N.
Euclidean algorithms (Basic and Extended)