# Recursion.docx - Student of Fortune

```					1. The recur method prints a sequence of digits on a single line.

public static void recur (int n)
{
if(n==1)
{
System.out.print(n);
}
else
{
System.out.print(n);
recur(n - 1);
}
}

What does the method call recur(5) display?

2. The method factorial returns the product of all integers between 1 and n inclusive.

For example:
If n = 10, factorial should return 3628800, since 10! = 10*9*8*7*6*5*4*3*2*1 =
3628800.

Write a recursive version of factorial.

(Remember: it's important to identify a base case for your recursive definition, so that

Public int factorial (int n){

}//end method

3. The method sumN returns the sum of the integers from 1 to n.

For example:
If n = 5, then sumN should return 15, since 5+4+3+2+1 = 15.

Write a recursive version of sumN.
(Remember: it's important to identify and include a nonrecursive base case, so that your
method will terminate properly).

Public int sumN (int n){
}//end method

4. The rowPrint method prints a sequence of digits on a single line.

public static void rowPrint (int n)
{
if(n==1)
{
System.out.print(n);
}
else
{
rowPrint(n - 1);
System.out.print(n);
}
}

What does the method call rowPrint(3) display?

5. public static void recur2(int n)
{
if(n<=0)
{
return;
}
else
{
recur2(n - 2);
System.out.print(n);
}
}

What does the method call recur2(2) display?

6. Here's another recur2 problem. Remember: the recur2 method prints a sequence of digits on
a single line.

public static void recur2 (int n)
{
if(n<=0)
{
return;
}
else
{
recur2(n - 2);
System.out.print(n);
}
}

What does the method call recur2(5) display?

7. The method sumEvens returns the sum of the numbers from 1 to n that are even (that is,
divisible by 2).

For example:
If n = 10, sumEvens should return 30, since the numbers from 1 to 10 that are evens are: 2, 4,
6, 8 and 10.

Write a recursive version of sumEvens.
(Remember that it's important to identify the base case so that your method will terminate
properly).

Public int sumEvens(int n){
}//end method

8. The method sumD3 returns the sum of the numbers from 1 to n that are divisible by 3.

For example:
If n = 10, sumD3 should return 18, since the numbers from 1 to 10 that are divisible by 3 are: 3,
6, and 9.

Write a recursive version of sumD3.
(Remember that it's important to identify the base case so your method will terminate
properly).
Public int sumD3(int n){

}//end method

9. The method sumNSquares calculates the sum of the squares of consecutive integers from 1 to
n, where n is a positive integer.

For example:

If n = 5, sumNSquares should return 55, since
(1*1) + (2*2) + (3*3) + (4*4) + (5*5) = 55.

Write a recursive version of sumNSquares. (Remember that it's important to identify the base
case so that your method will terminate properly).
public int sumNSquares(int n) {
} //end method

10. The method sumNSquaresD3 returns the sum of the squares of consecutive integers from 1 to
n that are divisible by 3, given that n is a positive integer.

For example:
If n = 6, sumNSquaresD3 should return 45, since the squares are:
(1*1) = 1, (2*2) = 4, (3*3) = 9, (4*4) = 16, (5*5) = 25, (6*6) = 36
but only 9 and 36 are divisible by 3, and 9 + 36 = 45.

Write a recursive version of sumNSquaresD3.
(Remember that it's important to identify the base case so your method will terminate
properly).

public int sumNSquaresD3 (int n) {
} //end method

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 144 posted: 2/27/2012 language: pages: 4