Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n in C++
In this problem, we are given two numbers X and n, which denote a mathematical series. Our task is to create a program to find the sum of the series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n.
Let’s take an example to understand the problem,
Input
x = 2 , n = 4
Output
Explanation −
sum= 1 + 2/1 + (2^2)/2 + (2^3)/3 + (2^4)/4
= 1 + 2 + 4/2 + 8/3 + 16/4
= 1 + 2 + 2 + 8/3 + 4
= 9 + 8/3
= 11.666.
A simple solution is to create the series and find the sum using the base value x and range n. Then return the sum.
Example
Program to illustrate the working of our solution,
Live Demo
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
double calcSeriesSum(int x, int n) {
double i, total = 1.0;
for (i = 1; i <= n; i++)
total += (pow(x, i) / i);
return total;
}
int main() {
int x = 3;
int n = 6;
cout<<"Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^"<<n<<"/"<<n<<" is "<<setprecision(5) <<calcSeriesSum(x, n);
return 0;
}
Output
Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^6/6 is 207.85
Related Articles
- Program to find sum of 1 + x/2! + x^2/3! +…+x^n/(n+1)! in C++
- Check whether the following are quadratic equations:(i) \( (x+1)^{2}=2(x-3) \)(ii) \( x^{2}-2 x=(-2)(3-x) \)(iii) \( (x-2)(x+1)=(x-1)(x+3) \)(iv) \( (x-3)(2 x+1)=x(x+5) \)(v) \( (2 x-1)(x-3)=(x+5)(x-1) \)(vi) \( x^{2}+3 x+1=(x-2)^{2} \)(vii) \( (x+2)^{3}=2 x\left(x^{2}-1\right) \)(viii) \( x^{3}-4 x^{2}-x+1=(x-2)^{3} \)
- Determine which of the following polynomials has \( (x+1) \) a factor:(i) \( x^{3}+x^{2}+x+1 \)(ii) \( x^{4}+x^{3}+x^{2}+x+1 \)(iii) \( x^{4}+3 x^{3}+3 x^{2}+x+1 \)(iv) \( x^{3}-x^{2}-(2+\sqrt{2}) x+\sqrt{2} \)
- 1. Factorize the expression \( 3 x y - 2 + 3 y - 2 x \)A) \( (x+1),(3 y-2) \)B) \( (x+1),(3 y+2) \)C) \( (x-1),(3 y-2) \)D) \( (x-1),(3 y+2) \)2. Factorize the expression \( \mathrm{xy}-\mathrm{x}-\mathrm{y}+1 \)A) \( (x-1),(y+1) \)B) \( (x+1),(y-1) \)C) \( (x-1),(y-1) \)D) \( (x+1),(y+1) \)
- Solve for x:$\frac{1}{( x-1)( x-2)} +\frac{1}{( x-2)( x-3)} =\frac{2}{3} \ ,\ x\neq 1,2,3$
- If \( x+\frac{1}{x}=3 \), calculate \( x^{2}+\frac{1}{x^{2}}, x^{3}+\frac{1}{x^{3}} \) and \( x^{4}+\frac{1}{x^{4}} \).
- Simplify each of the following products:\( (x^{3}-3 x^{2}-x)(x^{2}-3 x+1) \)
- Simplify:$(x^3 - 2x^2 + 3x - 4) (x - 1) - (2x - 3) (x^2 - x + 1)$
- Solve for $x$:$\frac{x-1}{x-2}+\frac{x-3}{x-4}=3\frac{1}{3}, x≠2, 4$
- If \( x-\frac{1}{x}=3+2 \sqrt{2} \), find the value of \( x^{3}- \frac{1}{x^{3}} \).
- Simplify the following :$( 3 x^2 + 5 x - 7 ) (x-1) - ( x^2 - 2 x + 3 ) (x + 4)$
- If \( x^{2}+\frac{1}{x^{2}}=51 \), find the value of \( x^{3}-\frac{1}{x^{3}} \).
- If \( x^{2}+\frac{1}{x^{2}}=98 \), find the value of \( x^{3}+\frac{1}{x^{3}} \).
- Use the Factor Theorem to determine whether \( g(x) \) is a factor of \( p(x) \) in each of the following cases:(i) \( p(x)=2 x^{3}+x^{2}-2 x-1, g(x)=x+1 \)(ii) \( p(x)=x^{3}+3 x^{2}+3 x+1, g(x)=x+2 \)(iii) \( p(x)=x^{3}-4 x^{2}+x+6, g(x)=x-3 \)
- If \( x^{4}+\frac{1}{x^{4}}=194 \), find \( x^{3}+\frac{1}{x^{3}}, x^{2}+\frac{1}{x^{2}} \) and \( x+\frac{1}{x} \)
Kickstart Your Career
Get certified by completing the course
Get Started