Get Answers to all your Questions

header-bg qa

Solve the series and print the sum 1-x^2/2!+x^3/3!-x^4/4!.........x^n/n! in C#

Answers (1)

best_answer

// 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); 
     
 

 

Posted by

Deependra Verma

View full answer