How would you write the program for part B? 4. Do two stars (**) as a complete J
ID: 3570090 • Letter: H
Question
How would you write the program for part
B?
Explanation / Answer
package dd.nhh;
public class NumberSeriesExample {
/**
* @param args
*/
public static void main(String[] args) {
int x = sum(5);
System.out.println(x); // 6 (output)
}
static int sum(int t)
{
int result;
if(t==1)
return 2; //base case declaration
result = 1+sum(t-1);
return result;
}
}
input: 5
output:
sum(5) =1+sum(4)=...=1+1+1+1+sum(1) = 4+2 = 6 (Answer) *
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.