Sum of series 2/3 – 4/5 + 6/7 – 8/9 + ——- upto n terms
Given the value of n, find the sum of the series (2 / 3) – (4 / 5) + (6 / 7) – (8 / 9) + – – – – – – – upto n terms.
Examples :
Input : n = 5
Output : 0.744012
Series : (2 / 3) - (4 / 5) + (6 / 7) - (8 / 9) + (10 / 11)
Input : n = 7
Output : 0.754268
Series : (2 / 3) - (4 / 5) + (6 / 7) - (8 / 9) +
(10 / 11) - (12 / 13) + (14 / 15)
C++
// C++ program to find // sum of given series #include <bits/stdc++.h> using namespace std; // Function to find sum of series // up-to n terms double seriesSum(int n) { // initializing counter by 1 int i = 1; // variable to calculate result double res = 0.0; bool sign = true; // while loop until nth term // is not reached while (n > 0) { n--; // boolean type variable // for checking validation if (sign) { sign = !sign; res = res + (double)++i / ++i; } else { sign = !sign; res = res - (double)++i / ++i; } } return res; } // Driver Code int main() { int n = 5; cout << seriesSum(n); return 0; } |
chevron_right
filter_none
Java
// Java program to find // sum of given series import java.io.*; class GFG { // Function to find sum of series // up-to n terms static double seriesSum(int n) { // initializing counter by 1 int i = 1; // variable to calculate result double res = 0.0; boolean sign = true; // while loop until nth term // is not reached while (n > 0) { n--; // boolean type variable // for checking validation if (sign) { sign = !sign; res = res + (double)++i / ++i; } else { sign = !sign; res = res - (double)++i / ++i; } } return res; } // Driver Code public static void main (String[] args) { int n = 5; System.out.print(seriesSum(n)); } } // This code is contributed by vt_m |
chevron_right
filter_none
Python3
# Python3 program to find # sum of given series # Function to find # sum of series # up-to n terms def seriesSum(n): # initializing # counter by 1 i = 1; # variable to # calculate result res = 0.0; sign = True; # while loop until nth # term is not reached while (n > 0): n = n - 1; # boolean type variable # for checking validation if (sign): sign = False; res = res + (i + 1) / (i + 2); i = i + 2; else: sign = True; res = res - (i + 1) / (i + 2); i = i + 2; return res; # Driver Code n = 5; print(round(seriesSum(n), 6)); # This code is contributed # by mits |
chevron_right
filter_none
C#
// C# program to find // sum of given series using System; class GFG { // Function to find sum of // series up-to n terms static double seriesSum(int n) { // initializing counter by 1 int i = 1; // variable to calculate result double res = 0.0; bool sign = true; // while loop until nth term // is not reached while (n > 0) { n--; // boolean type variable // for checking validation if (sign) { sign = !sign; res = res + (double)++i / ++i; } else { sign = !sign; res = res - (double)++i / ++i; } } return res; } // Driver Code public static void Main () { int n = 5; Console.Write(seriesSum(n)); } } // This code is contributed by vt_m |
chevron_right
filter_none
PHP
<?php // PHP program to find // sum of given series // Function to find sum of series // up-to n terms function seriesSum($n) { // initializing counter by 1 $i = 1; // variable to calculate result $res = 0.0; $sign = true; // while loop until nth term // is not reached while ($n > 0) { $n--; // boolean type variable // for checking validation if ($sign) { $sign = !$sign; $res = $res + (double)++$i / ++$i; } else { $sign = !$sign; $res = $res - (double)++$i / ++$i; } } return $res; } // Driver Code $n = 5; echo(seriesSum($n)); // This code is contributed by Ajit. ?> |
chevron_right
filter_none
Output :
0.744012
Recommended Posts:
- Sum of the series (1*2) + (2*3) + (3*4) + ...... upto n terms
- Sum of the series 1 / 1 + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + - - - - - - upto n terms.
- Sum of the series 0.7, 0.77, 0.777, ... upto n terms
- Find sum of the series ?3 + ?12 +......... upto N terms
- Find the sum of series 0.X + 0.XX + 0.XXX +... upto k terms
- Find Sum of Series 1^2 - 2^2 + 3^2 - 4^2 ..... upto n terms
- Find the sum of series 3, -6, 12, -24 . . . upto N terms
- Find sum of the series 1+22+333+4444+...... upto n terms
- Find the sum of the series 1+11+111+1111+..... upto n terms
- Program to find the sum of the series 23+ 45+ 75+..... upto N terms
- Find n terms of Fibonacci type series with given first two terms
- Sum of the Tan(x) expansion upto N terms
- Sum of Fibonacci numbers at even indexes upto N terms
- Program to get the Sum of series: 1 - x^2/2! + x^4/4! -.... upto nth term
- Sum of first n terms of a given series 3, 6, 11, .....
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



