In: Computer Science
s(i) = 21+ 24+47+410+…+ i+23i+1+i+13i+1
i = 0, 1, 2, 3, …
When i is even, the term is i+23i+1
When i is odd, the term is i+13i+1
In the main method, display the s(i) for i = 0, 1, 2, 3, 4, 5
Implement the program as follows:
Program: TestSumSeries.java
public class TestSumSeries{ /* define the class TestSumSeries */
public static int CalcSum(int i){ /* define a static method CalcSum() that accepts integer, i */
if(i==0){ /* if i = 0, then return 1 */
return 1;
}
else if(i%2==0){ /* if i is even, then recursively call CalcSum() and return i+23i+1 + CalcSum(i-1) */
return i+23*i+1 + CalcSum(i-1);
}
else{ /* if i is odd, then recursively call CalcSum() and return i+13i+1 + CalcSum(i-1) */
return i+13*i+1 + CalcSum(i-1);
}
}
public static void main(String[] args){ /* define main() method */
for(int i=0;i<6;i++){ /* for i varies from 0 to 5 */
System.out.println("s(" + i + ") = " + CalcSum(i)); /* print s(i) for i = 0, 1, 2, 3, 4, 5 */
}
}
}
Screenshot:
Output:
Please don't forget to give a Thumbs Up.