Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Consider the following method: public static int mystery(int x, int y){ if (x

ID: 3674634 • Letter: 1

Question

1. Consider the following method:

public static int mystery(int x, int y){

if (x<y){

}else{

}

}

return x;

return mystery(x-y,y);

What values are returned for each of the following calls?

a. mystery(7,12)

b. mystery(37,8)

c. mystery(8,2)

2. Write a recursive method called printN() that takes one int parameter and prints the

number from 1 to n. Do not use loops to print the numbers.

3. Write a recursive method called fib() that takes an int parameter and that prints the

Fibonacci number at that location in the series. The Fibonacci sequence is a sequence of

numbers in which the first two numbers are 1 and each subsequent number is sum of the

previous two Fibonacci numbers. The sequence is 1,1,2,3,5,8,13,21, and so on. So fib(4)

should print 3, fib(6) should print 8, fib(8) should print 21, fib(10) should print 55 and so

on.

Explanation / Answer

1.

a. 7

b. 5

c. 0

2.

Program:

#include<iostream>
using namespace std;
void printN(int n){
   if (n>=2)
       printN(n-1);
   cout<<endl<<n;
}

int main(){
       printN(10);      
   return 0;
}

3.

#include<stdio.h>

int Fibonacci(int);

main()
{

printf("%d",Fibonacci(8));

return 0;
}

int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}