Write a recursive method named series (int a, int n) that takes as a parameter an integer that may compute the following series:
-2, 7, 52, 2702… if a = -2 and n = 4
Then, write a full program to solve the above problem using recursion.
public class Main {
static int S(int a, int n){
if(a==0){
System.out.print(0+"");
return 0;
}
else if(a==1){
return a * n;
}
else if(n==0){
return a * 1;
}
else{
System.out.print(a * S(a, n-1)+"");
return a * S(a, n-1);
}
}
public static void main(String[] args) {
System.out.print("-2, 7, 52, 2702 ");
S(-2,4);
}
}
Comments
tq so much this help me
Leave a comment