Given a number of terms the task is to calculate the value upto ‘n’ terms of the below given series using recursion.
Series: (1/2!) + (2/3!) + (3/4!) + (4/5!) + …………… + ‘n’ terms
Java
import java.util.*;
public class RecSeries
{
public double getFact(int n)
{
if(n==1)
{
return 1;
}
else
{
return n*getFact(n-1);
}
}
public double getSum(int n)
{
if(n==0)
{
return 0;
}
else
{
return (n/getFact(n+1))+getSum(n-1);
}
}
public static void main(String args[])
{
int n=0;
double s=0.0;
Scanner sc=new Scanner(System.in);
RecSeries ob=new RecSeries();
System.out.print("Enter number of terms: ");
n=sc.nextInt();
s=ob.getSum(n);
System.out.println("Result: "+s);
}
}Java