This is a mathematical series program where a user must enter the number of terms up to which the sum of the series is to be found. Following this, we also need the value of x, which forms the base of the series.
Examples:
Input : x = 9, n = 10 Output : -5.1463 Input : x = 5, n = 15 Output : 0.2837
Simple approach :
We use two nested loops to compute factorial and use power function to compute power.
C
// C program to get the sum of the series #include <math.h> #include <stdio.h> // Function to get the series double Series(double x, int n) { double sum = 1, term = 1, fct, j, y = 2, m; // Sum of n-1 terms starting from 2nd term int i; for (i = 1; i < n; i++) { fct = 1; for (j = 1; j <= y; j++) { fct = fct * j; } term = term * (-1); m = term * pow(x, y) / fct; sum = sum + m; y += 2; } return sum; } // Driver Code int main() { double x = 9; int n = 10; printf("%.4f", Series(x, n)); return 0; } |
Java
// Java program to get the sum of the series import java.io.*; class MathSeries { // Function to get the series static double Series(double x, int n) { double sum = 1, term = 1, fct, j, y = 2, m; // Sum of n-1 terms starting from 2nd term int i; for (i = 1; i < n; i++) { fct = 1; for (j = 1; j <= y; j++) { fct = fct * j; } term = term * (-1); m = Math.pow(x, y) / fct; m = m * term; sum = sum + m; y += 2; } return sum; } // Driver Code public static void main(String[] args) { double x = 3; int n = 4; System.out.println(Math.round(Series(x, n) * 10000.0) / 10000.0); } } |
Python3
# Python3 code to get the sum of the series import math # Function to get the series def Series( x , n ): sum = 1 term = 1 y = 2 # Sum of n-1 terms starting from 2nd term for i in range(1,n): fct = 1 for j in range(1,y+1): fct = fct * j term = term * (-1) m = term * math.pow(x, y) / fct sum = sum + m y += 2 return sum # Driver Code x = 9n = 10print('%.4f'% Series(x, n)) # This code is contributed by "Sharad_Bhardwaj". |
C#
// C# program to get the sum of the series using System; class GFG { // Function to get the series static double Series(double x, int n) { double sum = 1, term = 1, fct, j, y = 2, m; // Sum of n-1 terms starting from 2nd term int i; for (i = 1; i < n; i++) { fct = 1; for (j = 1; j <= y; j++) { fct = fct * j; } term = term * (-1); m = Math.Pow(x, y) / fct; m = m * term; sum = sum + m; y += 2; } return sum; } // Driver Code public static void Main() { double x = 9; int n = 10; Console.Write(Series(x, n) * 10000.0 / 10000.0); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to get the // sum of the series // Function to get the series function Series($x, $n) { $sum = 1; $term = 1; $fct; $j; $y = 2; $m; // Sum of n-1 terms starting // from 2nd term for ($i = 1; $i < $n; $i++) { $fct = 1; for ($j = 1; $j <= $y; $j++) { $fct = $fct * $j; } $term = $term * (-1); $m = $term * pow($x, $y) / $fct; $sum = $sum + $m; $y += 2; } return $sum; } // Driver Code $x = 9; $n = 10; $precision = 4; echo substr(number_format(Series($x, $n), $precision + 1, '.', ''), 0, -1); // This code is contributed by Ajit. ?> |
Output:
-5.1463
Efficient approach :
We can avoid inner loop and use of power function by using values computed in previous iteration.
CPP
// C++ program to get the sum of the series #include <math.h> #include <stdio.h> // Function to get the series double Series(double x, int n) { double sum = 1, term = 1, fct = 1, p = 1, multi = 1; // Computing sum of remaining n-1 terms. for (int i = 1; i < n; i++) { fct = fct * multi * (multi+1); p = p*x*x; term = (-1) * term; multi += 2; sum = sum + (term * p)/fct; } return sum; } // Driver Code int main() { double x = 9; int n = 10; printf("%.4f", Series(x, n)); return 0; } |
Java
// Java program to get // the sum of the series import java.io.*; class GFG { // Function to get // the series static double Series(double x, int n) { double sum = 1, term = 1, fct = 1; double p = 1, multi = 1; // Computing sum of remaining // n-1 terms. for (int i = 1; i < n; i++) { fct = fct * multi * (multi + 1); p = p * x * x; term = (-1) * term; multi += 2; sum = sum + (term * p) / fct; } return sum; } // Driver Code public static void main(String args[]) { double x = 9; int n = 10; System.out.printf("%.4f", Series(x, n)); } } // This code is contributed by Nikita Tiwari. |
Python3
# Python3 code to get the sum of the series # Function to get the series def Series(x, n): sum = 1 term = 1 fct = 1 p = 1 multi = 1 # Computing sum of remaining n-1 terms. for i in range(1, n): fct = fct * multi * (multi+1) p = p*x*x term = (-1) * term multi += 2 sum = sum + (term * p)/fct return sum # Driver Code x = 9n = 10print('%.4f'% Series(x, n)) # This code is contributed by "Sharad_Bhardwaj". |
C#
// C# program to get // the sum of the series using System; class GFG { // Function to get // the series static float Series(double x, int n) { double sum = 1, term = 1, fct = 1; double p = 1, multi = 1; // Computing sum of remaining // n-1 terms. for (int i = 1; i < n; i++) { fct = fct * multi * (multi + 1); p = p * x * x; term = (-1) * term; multi += 2; sum = sum + (term * p) / fct; } return (float)sum; } // Driver Code public static void Main() { double x = 9; int n = 10; Console.Write(Series(x, n)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to get // the sum of the series // Function to get the series function Series($x, $n) { $sum = 1; $term = 1; $fct = 1; $p = 1; $multi = 1; // Computing sum of // remaining n-1 terms. for ($i = 1; $i < $n; $i++) { $fct = $fct * $multi * ($multi + 1); $p = $p * $x * $x; $term = (-1) * $term; $multi += 2; $sum = $sum + ($term * $p) / $fct; } return $sum; } // Driver Code $x = 9; $n = 10; $precision = 4; echo substr(number_format(Series($x, $n), $precision + 1, '.', ''), 0, -1); // This code is contributed by Ajit. ?> |
Output:
-5.1463
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:
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Program for sum of cosh(x) series upto Nth term
- Find the Nth term of the series where each term f[i] = f[i - 1] - f[i - 2]
- Find Nth term of the series where each term differs by 6 and 2 alternately
- Program to print tetrahedral numbers upto Nth term
- Program to print pentatope numbers upto Nth term
- Nth term of a sequence formed by sum of current term with product of its largest and smallest digit
- Sum of series till N-th term whose i-th term is i^k - (i-1)^k
- First term from given Nth term of the equation F(N) = (2 * F(N - 1)) % 10^9 + 7
- Nth term of given recurrence relation having each term equal to the product of previous K terms
- Program to find Nth term in the given Series
- Program to find the Nth term of the series 3, 7, 13, 21, 31.....
- Program to find Nth term of series 1, 3, 12, 60, 360…
- Program to find the Nth term of series -1, 2, 11, 26, 47......
- Program to find Nth term in the series 0, 0, 2, 1, 4, 2, 6, 3, 8,...
- Program to find Nth term of series 9, 23, 45, 75, 113...
- Program to find Nth term in the series 0, 2, 1, 3, 1, 5, 2, 7, 3,…
- Program to find the Nth term of the series 3, 20, 63, 144, 230, ……
- Program to find the Nth term of series 5, 10, 17, 26, 37, 50, 65, 82, ...
- Program to find the Nth term of series 0, 4, 14, 30, 51, 80, 114, 154, 200, ...
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.
Improved By : jit_t

