Program to get the Sum of series: 1 – x^2/2! + x^4/4! -.... upto nth term

03 Jun, 2022
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
Java
Python3
C#
PHP
Javascript
// C++ program to get the sum of the series#include <bits/stdc++.h>using namespace std; // Function to get the seriesdouble 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 Codeint main(){    double x = 9;    int n = 10;    cout << Series(x, n);    return 0;}// This code is contributed by Samim Hossain Mondal.
// C program to get the sum of the series#include <math.h>#include <stdio.h> // Function to get the seriesdouble 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 Codeint main(){    double x = 9;    int n = 10;    printf("%.4f", Series(x, n));    return 0;}
// Java program to get the sum of the seriesimport 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 code to get the sum of the seriesimport math # Function to get the seriesdef 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 Codex = 9n = 10print('%.4f'% Series(x, n)) # This code is contributed by "Sharad_Bhardwaj".
// C# program to get the sum of the seriesusing 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 program to get the// sum of the series // Function to get the seriesfunction 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.?>
<script> // Javascript program to get the sum of the series // Function to get the seriesfunction Series(x, n){    let sum = 1, term = 1, fct, j, y = 2, m;     // Sum of n-1 terms starting from 2nd term    let i;    for(i = 1; i < n; i++)    {        fct = 1;        for(j = 1; j <= y; j++)        {            fct = fct * j;        }        term = term * (-1);        m = term * Math.pow(x, y) / fct;        sum = sum + m;        y += 2;    }    return sum;} // Driver Codelet x = 9;let n = 10; document.write(Series(x, n).toFixed(4)); // This code is contributed by Surbhi Tyagi. </script>
-5.1463
Time Complexity: O(n * y)
Auxiliary Space: O(1)
Efficient approach : We can avoid inner loop and use of power function by using values computed in previous iteration.
C++
C
Java
Python3
C#
PHP
Javascript
// C++ program to get the sum of the series#include <math.h>#include <stdio.h> // Function to get the seriesdouble 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 Codeint main(){    double x = 9;    int n = 10;    printf("%.4f", Series(x, n));    return 0;}
#include <stdio.h>#include <stdlib.h>#include <limits.h>#include <limits.h> 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;} int main(){  double x = 9;  int n = 10;  printf("%.4f", Series(x, n));  return 0;} // This code is contributed by abhinavprkash.
// Java program to get// the sum of the seriesimport 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 code to get the sum of the series # Function to get the seriesdef 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 Codex = 9n = 10print('%.4f'% Series(x, n)) # This code is contributed by "Sharad_Bhardwaj".
// C# program to get// the sum of the seriesusing 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 program to get// the sum of the series // Function to get the seriesfunction 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.?>
<script>// Javascript program to get// the sum of the series     // Function to get    // the series    function Series(x , n) {        var sum = 1, term = 1, fct = 1;        var p = 1, multi = 1;         // Computing sum of remaining        // n-1 terms.        for (let 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             var x = 9;        var n = 10;         document.write(Series(x, n).toFixed(4)); // This code is contributed by Amit Katiyar</script>
-5.1463
Time Complexity: O(n)
Auxiliary Space: O(1)
jit_t
amit143katiyar
surbhityagi15
samim2000
abhinavprkash
factorial
series
Mathematical
Mathematical
series
factorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operators in C / C++
Find minimum number of coins that make a given value
Minimum number of jumps to reach end
Algorithm to solve Rubik's Cube
The Knight's tour problem | Backtracking-1
Modulo Operator (%) in C/C++ with Examples
Modulo 10^9+7 (1000000007)
Program for factorial of a number
Program to print prime numbers from 1 to N.
Program to find sum of elements in a given array