Sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + ...... + (1+3+5+7+...+(2n-1)) in C++

C++Server Side ProgrammingProgramming

In this problem, we are given a number n. Our task is to create a program to find the sum of series 1 + (1+3) + (1+3+5) + (1+3+5+7) + …… + (1+3+5+7+…+(2n-1)). 

Let’s take an example to understand the problem, 

Input:   n = 5

Output:  55

So, according to the question suppose a user gives us a number ‘n’ and we have to add series 1 + (1+3) + (1+3+5) + (1+3+5+7) + …… + (1+3+5+7+…+(2n-1)).

Let's understand the meaning of this series better first.

We take n=1, then the series becomes 1.

We take n=2, then the series becomes 1+ (1+3) because the value of last term 2n-1 can be calculated as, 2 times 2 exceeding 1, which is 3.

Value of n
2n-1
Series becomes
1
1
1
2
3
1+ (1+3)
3
5
1+ (1+3) + (1+3+5)
4
7
1+ (1+3) + (1+3+5) + (1+3+5+7)

Coming to the solution to the problem, it can be solved in two ways. One will be mathematical where an expression of total sum can be obtained and hence no loops are required. The other will be to apply two loops in the code.

Direct approach using Loops

As it can be seen that the term of the series  1 + (1+3) + (1+3+5) + (1+3+5+7) + …… + (1+3+5+7+…+(2n-1)) is a series itself. Hence we will use nested loops. The outer loop will calculate the second term. While the inner loop will be used to calculate the term itself.

Example

Live Demo

#include<stdio.h>

int calcSum(int n){
   int sum = 0;
   for (int i = 1; i <= n; i++) {
      // the first value of the term is always 1
      int value = 1;
      for (int j = 1; j <= i; j++) {
         sum += value;
         // next term
         value += 2;
      }
   }
   return sum;
}

int main(){
   int n = 35;
   printf("The sum of the series upto %d is %d ", n , calcSum(n));

}

Output

The sum of the series upto 35 is 14910

Working of the program:

  • The user enters the value of n. Let’s say, 2.
  • A variable named “sum” is declared with an initial value as 0
  • When i=1, the for the condition i<=n is true and hence loop will work
    • The value of the variable “ft” is 1.
    • The first value of j is 1. The condition is true as the value of j is equal to the value of i, which is 1. So the j loop works
    • The value of ft is added to the sum. Hence sum becomes 0+1 equal to 1.
    • The value of ft is modified and increased by 2, so its new value is 1+2 = 3
    • The value of j increases by 1 and becomes 2.
    • But now the condition for inside for loop is false, as j>i now. Hence the j loop is exited.
  • Now the value of i increases by 1 and becomes 2, so i=2 and the condition is true i<= n, hence enters the loop
    • The value of variable “ft” is again defined as 1
    • When the value of j is 1, the loop will work as j<i or 1<2.
    • The value of ft is added to the sum. The value of the sum is 1 already. So the new value of the sum is 1+1 = 2
    • The value of ft is modified and increased by 2, hence the new value of ft becomes 1+2 =3.
    • The value of j increases by 1 and becomes 2. The for loop condition is true as j is equal to i.
    • The value of ft is added to the sum. The value of the sum is 2 already. So the new value of the sum is 2+3 = 5
    • The value of ft is modified and increased by 2, hence the new value of ft becomes 3+2 =5.
    • The loop exists
  • Now the value of i increases by 1 and becomes 3, so i=3 and the condition is false i<= n, hence exits the loop.
  • Prints the message and value of sum on the screen

Mathematical solution:

By finding a mathematical solution to the question and then writing the code, will simplify our code to a great extent.

Let the general term Tn  of the series,

Before progressing further, we should know that series 1+3+5+7+9…..(2n-1) will have a sum of n2, and the series

12+22+32+42….n2  will have a sum of i  =

Program to illustrate the working of our code

Example

Live Demo

#include<stdio.h>

int calcSum(int n){
   // required sum
   return (( (n) * (n + 1) * (2*n + 1 ) )/6 ) ;
}

int main(){
int n = 35;
   printf("The sum of the series upto %d is %d ", n , calcSum(n));

}

Output

The sum of the series upto 35 is 14910

Working of the above code: 

For example, let us assume the user entered the value of n as 2, then the value of 2n-1 is 3 and the series becomes 1+ (1+3).

But let us understand and obtain the sum through code.

  • The function sum() is called with value 2
  • The function calculates the value of  as 5 and returns it to the main function.
  • The message with the answer is printed on the screen.
raja
Published on 27-Jan-2021 09:25:24
Advertisements