Given a number of terms the task is to calculate the value upto ‘n’ terms of the below given series using recursion.
Series: 12 + 22 + 32 + 42 + …………… + ‘n’ terms
Java
import java.util.*;
public class RecSeries
{
public int getSum(int n)
{
if(n==1)
{
return 1;
}
else
{
return (n*n)+getSum(n-1);
}
}
public static void main(String args[])
{
int n=0,s=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