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++
#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;
}
};
int main()
{
gfg g;
int x = 2;
int n = 5;
std::cout << boost::format("%.2f") % g.sum(x,n);
return 0;
}
|
C
#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;
}
int main()
{
int x = 2;
int n = 5;
printf("%.2f", sum(x, n));
return 0;
}
|
Java
import static java.lang.Math.pow;
class GFG
{
static 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;
}
public static void main(String[] args)
{
int x = 2;
int n = 5;
System.out.printf("%.2f", sum(x, n));
}
}
|
Python3
def SUM(x, n):
total = 1
for i in range(1, n + 1):
total = total + ((x**i)/i)
return total
x = 2
n = 5
s = SUM(x, n)
print(round(s, 2))
|
C#
using System;
class GFG
{
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;
}
public static void Main()
{
int x = 2;
int n = 5;
Console.WriteLine(sum(x, n));
}
}
|
PHP
<?php
function sum($x, $n)
{
$i; $total = 1.0;
for ($i = 1; $i <= $n; $i++)
$total = $total +
(pow($x, $i) / $i);
return $total;
}
$x = 2;
$n = 5;
echo(sum($x, $n));
?>
|
Output :
18.07
Method 2 (Optimized) We can avoid use of pow() function and reuse the previously computed power.
C
#include <math.h>
#include <stdio.h>
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;
}
int main()
{
int x = 2;
int n = 5;
printf("%.2f", sum(x, n));
return 0;
}
|
Java
class GFG
{
static 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;
}
public static void main(String[] args)
{
int x = 2;
int n = 5;
System.out.printf("%.2f", sum(x, n));
}
}
|
Python3
def sum(x, n):
total = 1.0
multi = x
for i in range(1, n+1):
total = total + multi / i
multi = multi * x
return total
x = 2
n = 5
print(round(sum(x, n), 2))
|
C#
using System;
class GFG
{
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;
}
public static void Main()
{
int x = 2;
int n = 5;
Console.WriteLine(sum(x, n));
}
}
|
PHP
<?php
function sum($x, $n)
{
$i; $total = 1.0; $multi = $x;
for ($i = 1; $i <= $n; $i++)
{
$total = $total + $multi / $i;
$multi = $multi * $x;
}
return $total;
}
$x = 2;
$n = 5;
echo(sum($x, $n));
?>
|
Output :
18.07
Recommended Posts:
- Sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + …… + (1+3+5+7+…+(2n-1))
- Sum of the series 1.2.3 + 2.3.4 + ... + n(n+1)(n+2)
- Sum of series (n/1) + (n/2) + (n/3) + (n/4) +.......+ (n/n)
- Sum of Series (n^2-1^2) + 2(n^2-2^2) +....n(n^2-n^2)
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2
- Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + ...... + (1+2+3+4+...+n)
- Sum of series 1*1*2! + 2*2*3! + ……..+ n*n*(n+1)!
- Value of the series (1^3 + 2^3 + 3^3 + ... + n^3) mod 4 for a given n
- Sum of series 1*1! + 2*2! + ……..+ n*n!
- Sum of the series 3, 20, 63, 144, ......
- Sum of the series 2 + (2+4) + (2+4+6) + (2+4+6+8) + …… + (2+4+6+8+….+2n)
- Sum of the Series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + . . . . .
- Sum of the series Kn + ( K(n-1) * (K-1)1 ) + ( K(n-2) * (K-1)2 ) + ....... (K-1)n
- Sum of the first N terms of the series 2,10, 30, 68,....
- Sum of the first N terms of the series 5,12, 23, 38....

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.