[go: up one dir, main page]

0% found this document useful (0 votes)
349 views5 pages

Apcs - Recursion Worksheet

The document contains code for various recursive functions and methods. It provides sample calls to the functions/methods and outputs the results. The functions demonstrate different types of recursion, including recursion with parameters, printing output, and string/array processing.

Uploaded by

api-355180314
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
349 views5 pages

Apcs - Recursion Worksheet

The document contains code for various recursive functions and methods. It provides sample calls to the functions/methods and outputs the results. The functions demonstrate different types of recursion, including recursion with parameters, printing output, and string/array processing.

Uploaded by

api-355180314
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Recursion Worksheet

Show the output of each block of code below.

1. What is returned by the call fun1(3) ? 2. What is output by the call fun2(3) ?

void fun2(int x)
int fun1(int x) {
{ if(x<1) System.out.println("\nDONE");
if (x<1) return x; else
else return x + fun1(x-1); {
} System.out.print(x);
fun2(x-1);
}
}

3. What is output by the fun3(4)? 4. What is returned by the call fun4(3,6)?

void fun3(int x) 5. What is returned by the call fun4(4,2)?


{
if(x<1) System.out.println("OUTPUT: ");
else int fun4( int x, int y)
{ {
fun3(x-1); if( y == 2) return x;
System.out.print(x); else return fun4( x, y - 1) + x;
} }
}

6. What is output by the call fun5(4) ? 7. What is returned by fun6(8)?

void fun5(int x)
{ int fun6(int x)
if(x<1) System.out.print("\n----\n"); {
else if (x<1) return x;
{ else return x + fun6(x-2);
System.out.print(x); }
fun5(x-1);
System.out.print(x);
}
}

8. What is returned by fun7(7,2)?


10. What is returned by fun8(2,8) ?
9. What is returned by fun7(5,5)?

int fun8(int x, int y)


int fun7( int x, int y) {
{ if(x<=1) return y;
if( y == 2) return y; else return fun8(x-1,y-1) + y;
else return fun7( x, y - 1) + x; }
}

11. What is returned by go(5)? 13. What is returned by fly(5)?

12. What is returned by go(3)?


int fly(int x)
{
int go(int x) if(x<1) return 1;
{ else return x + fly(x-4) + fly(x-1);
if(x<1) return 1; }
else return x + go(x-2) + go(x-3);
}
14. What is returned by boogie(5,10)? 15. What is output by the call mango(12)?

int boogie(int x, int y) void mango( int k )


{ {
if(y<2) if(k < 2 ) System.out.println();
return x; else
else {
return boogie(x,y-2) + x; if( k % 2 == 0 )
} {
System.out.print(k + " ");
mango(k - 1);
}
else mango(k - 1);
}
}

16. What is output by the call rain(5)?

void rain( int k )


{
if( k <= 0 ) return;
else
{
rain( k - 1 );
for( int j=0; j<k; j++)
System.out.print("# ");
System.out.println();
}
}

17. What is returned by chalupa("parangaricutirimicuaro",'a')?

int chalupa( String n, char c )


{
if( n.length() == 1 && n.charAt(0) == c ) return 1;
if( n.length() == 1 && n.charAt(0) != c ) return 0;
else
{
if( n.charAt(0) == c )
{
return 1 + chalupa( n.substring(1), c );
}
else return chalupa( n.substring(1), c );
}
}
/**
* Programs for Recursive Worksheet
* @author (Dominguez)
*/
public class Recursion
{
public static void main( String args[] )
{
System.out.println( "fun1(3) = " + fun1(3) + "\n" +
"fun4(3,6) = " + fun4(3,6) + "\n" +
"fun4(4,2) = " + fun4(4,2) + "\n" +
"fun6(8) = " + fun6(8) + "\n" +
"fun7(7,2) = " + fun7(7,2) + "\n" +
"fun7(5,5) = " + fun7(5,5) + "\n" +
"fun8(2,8) = " + fun8(2,8) + "\n" +
"go(5) = " + go(5) + "\n" +
"go(3) = " + go(3) + "\n" +
"fly(5) = " + fly(5) + "\n" +
"boogie(5,10) = " + boogie(5,10) + "\n" +
"chalupa(\"parangaricutirimicuaro\",\'a\') = " +
chalupa("parangaricutirimicuaro",'a') + "\n" );
System.out.println("\nfun2(3): "); fun2(3);
System.out.println("\n--------------------------");
System.out.println("fun3(4): "); fun3(4);
System.out.println("\n--------------------------");
System.out.println("fun5(4): "); fun5(4);
System.out.println("\n--------------------------");
System.out.println("mango(12):"); mango(12);
System.out.println("\n--------------------------");
System.out.println("rain(5):"); rain(5);
System.out.println("\n--------------------------");

static int fun1(int x)


{
if (x<1) return x;
else return x + fun1(x-1);
}

static void fun2(int x)


{
if(x<1) System.out.println("\nDONE");
else
{
System.out.print(x);
fun2(x-1);
}
}

static void fun3(int x)


{
if(x<1) System.out.println("OUTPUT: ");
else
{
fun3(x-1);
System.out.print(x);
}
}

static int fun4( int x, int y)


{
if( y == 2) return x;
else return fun4( x, y - 1) + x;
}
static void fun5(int x)
{
if(x<1) System.out.print("\n----\n");
else
{
System.out.print(x);
fun5(x-1);
System.out.print(x);
}
}

static int fun6(int x)


{
if (x<1) return x;
else return x + fun6(x-2);
}

static int fun7( int x, int y)


{
if( y == 2) return y;
else return fun7( x, y - 1) + x;
}

static int fun8(int x, int y)


{
if(x<=1) return y;
else return fun8(x-1,y-1) + y;
}

static int go(int x)


{
if(x<1) return 1;
else return x + go(x-2) + go(x-3);
}

static int fly(int x)


{
if(x<1) return 1;
else return x + fly(x-4) + fly(x-1);
}

static void mango( int k )


{
if(k < 2 ) System.out.println();
else
{
if( k % 2 == 0 )
{
System.out.print(k + " ");
mango(k - 1);
}
else mango(k - 1);
}
}

static int boogie(int x, int y)


{
if(y<2) return x;
else return boogie(x,y-2) + x;
}
static void rain( int k )
{
if( k <= 0 ) return;
else
{
rain( k - 1 );
for( int j=0; j<k; j++)
System.out.print("# ");
System.out.println();
}
}

static int chalupa( String n, char c )


{
if( n.length() == 1 && n.charAt(0) == c ) return 1;
if( n.length() == 1 && n.charAt(0) != c ) return 0;
else
{
if( n.charAt(0) == c ) return 1 + chalupa( n.substring(1), c );
else return chalupa( n.substring(1), c );
}
}
}

OUTPUT

fun1(3) = 6
fun4(3,6) = 15
fun4(4,2) = 4
fun6(8) = 20
fun7(7,2) = 2
fun7(5,5) = 17
fun8(2,8) = 15
go(5) = 16
go(3) = 7
fly(5) = 23
boogie(5,10) = 30
chalupa("parangaricutirimicuaro",'a') = 4

fun2(3):
321
DONE
--------------------------
fun3(4):
OUTPUT:
1234
--------------------------
fun5(4):
4321
----
1234
--------------------------
mango(12):
12 10 8 6 4 2
--------------------------
rain(5):
#
##
###
####
#####
--------------------------

You might also like