
- 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 / 1 + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + … + upto n terms in C++
Here, we are given an integer n. It defines the number of terms of the series 1/1 + ( (1+2)/(1*2) ) + ( (1+2+3)/(1*2*3) ) + … + upto n terms.
Our task is to create a program that will find the sum of series 1/1 + (1+2)/(1*2) + (1+2+3)/(1*2*3) + … upto n terms.
Let’s take an example to understand the problem,
Input
n = 3
Output
3.5
Explanation −(1/1) + (1+2)/(1*2) + (1+2+3)/(1*2*3) = 1 + 1.5 + 1 = 3.5
A simple solution to this problem is by looping from 1 to n. Then, add the values of the sum of i divided by product upto i.
Algorithm
Initialise result = 0.0, sum = 0, prod = 1 Step 1: iterate from i = 0 to n. And follow : Step 1.1: Update sum and product value i.e. sum += i and prod *= i Step 1.2: Update result by result += (sum)/(prod). Step 2: Print result.
Example
Program to illustrate the working of our solution,
#include <iostream> using namespace std; double calcSeriesSum(int n) { double result = 0.0 ; int sum = 0, prod = 1; for (int i = 1 ; i <= n ; i++) { sum += i; prod *= i; result += ((double)sum / prod); } return result; } int main() { int n = 12; cout<<"Sum of the series 1/1 + (1+2)/(1*2) + (1+2+3)/(1*2*3) + ... upto "<<n<<" terms is " <<calcSeriesSum(n) ; return 0; }
Output
Sum of the series 1/1 + (1+2)/(1*2) + (1+2+3)/(1*2*3) + ... upto 12 terms is 4.07742
- Related Articles
- Plus One in Python
- Examine whether root 2 plus 2 square is rational or irrational
- 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
- Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n) in C++
- Plus One Linked List in C++
- Cplus plus vs Java vs Python?
- What is the Cost plus pricing method?
- What is Unary Plus Operator in JavaScript?
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n – 1)^2
- Sum of the Series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + ... in C++\n
- C Program to check Plus Perfect Number

Advertisements