
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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,
#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++
- Plus One in Python
- LocalDateTime plus() method in Java
- Duration plus() method in Java
- LocalTime plus() method in Java
- Instant plus() method in Java
- LocalDate plus() method in Java
- 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} \)
- Plus One Linked List in C++
- Examine whether root 2 plus 2 square is rational or irrational
- Cplus plus vs Java vs Python?
- Solve for x:$\frac{1}{( x-1)( x-2)} +\frac{1}{( x-2)( x-3)} =\frac{2}{3} \ ,\ x\neq 1,2,3$
- What is the Cost plus pricing method?
- 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) \)

Advertisements