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.
package geometric;
public class Geometric {
static int series(int a, int n){
if(a==0){
System.out.println(0);
return 0;
}
else if(a==1){
return a * n;
}
else if(n==0){
return a * 1;
}
else{
System.out.println(a * series(a, n-1));
return a * series(a, n-1);
}
}
public static void main(String[] args) {
series(-2,4);
}
}
Comments
Leave a comment