Computer Science, asked by sakshamsooden, 7 months ago

Write a program to output a series 1+2/2*3 + 2+3/3*4 + 4+5/5*6 upto n terms (in java)

Answers

Answered by ADARSHrajraj36
0

Answer:

1st term = 1/2

2nd term = - 2/3

3rd term = 3/4

4th term = - 4/5

.

.

Nthe term = ((-1)N) * (N / (N + 1))

Therefore:

Nth term of the series \frac{1}{2} - \frac{2}{3} + \frac{3}{4} - \frac{4}{5} + ... = (-1)^{N}\times \frac{N}{N+1}

Then iterate over numbers in the range [1, N] to find all the terms using the above formula and compute their sum.

Below is the implementation of the above approach:

filter_none

edit

play_arrow

brightness_4

// C++ program for the above approach  

 

#include <bits/stdc++.h>  

using namespace std;  

 

// Function to find the sum of series  

void printSeriesSum(int N)  

{  

   double sum = 0;  

 

   for (int i = 1; i <= N; i++) {  

 

       // Generate the ith term and  

       // add it to the sum if i is  

       // even and subtract if i is  

       // odd  

       if (i & 1) {  

           sum += (double)i / (i + 1);  

       }  

       else {  

           sum -= (double)i / (i + 1);  

       }  

   }  

 

   // Print the sum  

   cout << sum << endl;  

}  

 

// Driver Code  

int main()  

{  

   int N = 10;  

 

   printSeriesSum(N);  

   return 0;  

}  

Output:

-0.263456

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

Recommended Posts:

Find the Sum of the series 1, 2, 3, 6, 9, 18, 27, 54, ... till N terms

Find the Sum of the series 1 + 1/3 + 1/5 + 1/7 + ... till N terms

Find the Sum of the series 1 + 2 + 9 + 64 + 625 + 7776 ... till N terms

Program to print the series 1, 9, 17, 33, 49, 73, 97... till N terms

Program to prin

Similar questions