Series 08

Given a number of terms and a number ‘x’ the task is to calculate the value upto ‘n’ terms of the below given series.

Series: 1 + (x1/2!) + (x2/3!) + (x3/4!) + (x4/5!) …………… + ‘n’ terms.

Note: Value of ‘x’ is to be given by user.

Java
import java.util.*;
public class Series
{
    public static void main(String args[])
    {
        int n=0,x=0,f=0;
        double result=0.0;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter a number: ");
        x=sc.nextInt();
        System.out.print("Enter number of terms: ");
        n=sc.nextInt();
        for(int i=1;i<=n;i++)
        {
            f=1;
            for(int j=1;j<=i;j++)
            {
                f=f*j;
            }
            result=result+(Math.pow(x,i-1)/f);
        }
        System.out.println("Result of the series: "+result);
    }
}
Java