
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sum of the Series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + ... in C++
In this problem, we are given a number n which is the nth term of the series 1/(1*2) + 1/(2*3) +…+ 1/(n*(n+1)). Our task is to create a program to find the sum of the series.
Let’s take an example to understand the problem,
Input
n = 3
Output
0.75
Explanation − sum = 1/(1*2) + 1/(2*3) + 1/(3*4) = ½ + ⅙+ 1/12 = (6+2+1)/12 = 9/12 = ¾ = 0.75
A simple solution to the problem is using the loop. And commuting value for each element of the series. Then add them to the sum value.
Algorithm
Initialize sum = 0 Step 1: Iterate from i = 1 to n. And follow : Step 1.1: Update sum, sum += 1/ ( i*(i+1) ) Step 2: Print sum.
Example
Program to illustrate the working of our solution,
#include <iostream> using namespace std; double calcSeriesSum(int n) { double sum = 0.0; for (int i = 1; i <= n; i++) sum += ((double)1/(i*(i+1))); return sum; } int main() { int n = 5; cout<<"Sum of the series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + ... is "<<calcSeriesSum(n); return 0; }
Output
Sum of the series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + ... is 0.833333
This solution is not much effective as it uses loops.
An effective approach to solve the problem is using the general formula for the sum of series.
The series is 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + … n-th terms is 1/n(n+1). an = 1/n(n+1) an = ((n+1) - n) /n(n+1) an = (n+1)/n(n+1) - n/ n(n+1) an = 1/n - 1/(n+1) sum of the series is sum = 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + … Changing each term as in above formula, sum = 1/1 - ½ + ½ - ⅓ + ⅓ - ¼ + ¼ -⅕ + …. 1/n - 1/(n+1) sum = 1 - 1/(n+1) sum = (n+1 -1) / (n+1) = n/(n+1)
Example
Program to illustrate the working of our solution,
#include <iostream> using namespace std; double calcSeriesSum(int n) { return ((double)n/ (n+1)); } int main() { int n = 5; cout<<"Sum of the series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + ... is "<<calcSeriesSum(n); return 0; }
Output
Sum of the series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + ... is 0.833333
- Related Questions & Answers
- Sum of the Series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + ... in C++\n
- Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n) in C++
- Program to find sum of series 1*2*3 + 2*3*4+ 3*4*5 + . . . + n*(n+1)*(n+2) in C++
- Sum of the series 1 / 1 + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + … + upto n terms in C++
- Program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n in C++
- C++ program to find the sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + … + (n*n)
- Sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + + (1+3+5+7+....+(2n-1)) in C++
- Sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + ...... + (1+3+5+7+...+(2n-1)) in C++
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2 in C++
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n – 1)^2
- Count number of trailing zeros in (1^1)*(2^2)*(3^3)*(4^4)*.. in C++
- C++ program to find the sum of the series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
- C++ Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! + …… n/n!
- C++ program to find the sum of the series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
- Python Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
- Java Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
Advertisements