Uploaded by nathanyuan420

recursion worksheet1 java aplus

advertisement
A+ Computer Science – Recursion Worksheet 1
DIRECTIONS : Fill in each blank with the correct answer/output. Assume each statement happens
in order and that one statement may affect the next statement. Some sections might print more
than once.
static void fun1(int x)
{
if(x>=1){
out.print(x);
fun1(x-1);
}
}
static void fun2(int x)
{
if(x<1){
out.print(x);
}else{
out.print(x);
fun2(x-1);
}
}
static int fun3(int x)
{
if (x<1)
return x;
else
return x + fun3(x-2);
}
static int
{
if( y ==
return
else
return
}
fun4( int x, int y)
2)
y;
fun4( x, y - 1) + x;
///////////////////////////////////
//runner code in the main of another class
fun1(5);
//line 1
1.
_______________
fun2(6);
//line 2
2.
_______________
System.out.println( fun3(4) );
//line 3
3.
_______________
System.out.println( fun4(3, 6) );
//line 4
4.
_______________
System.out.println( fun4(4 , 2));
//line 5
5.
_______________
© A+ Computer Science – Worksheet – www.apluscompsci.com
Download