AP Computer Science - Recursion Worksheet 3 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. public static int fun(int x) { if (x<1) return 1; else return x+fun(x-1)+fun(x-2); } public static int go(int x) { if (x<1) return 1; else return x+go(x-2)+go(x-3); } public static int fly(int x) { if (x<1) return 1; else return x+fly(x-4)+fly(x-1); } //runner code in the main of another class System.out.println("#1 " +fun(2)); System.out.println("#2 " +fun(6)); System.out.println("#3 " +go(5)); System.out.println("#4 " +go(3)); System.out.println("#5 " +fly(4)); System.out.println("#6 " +fly(5));
Added by James D.
Close
Step 1
fun(2) = 2 + fun(0) + fun(-1) fun(0) = 1 (since x < 1) fun(-1) = 1 (since x < 1) Show more…
Show all steps
Your feedback will help us improve your experience
Tarandeep Singh and 68 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
Consider the following recursive function fun(x, y). What is the value of fun(3, 3)? int fun(int x, int y) if (x == 0) return y; return fun(x - 1, x + y);
Kumareshwaran R.
Please provide a response in C++. Write code to complete PrintFactorial()'s recursive case. Sample output if userVal is 5: 5! = 5 * 4 * 3 * 2 * 1 = 120 #include <iostream> using namespace std; void PrintFactorial(int factCounter, int factValue){ int nextCounter; int nextValue; if (factCounter == 0) { // Base case: 0! = 1 cout << "1" << endl; } else if (factCounter == 1) { // Base case: Print 1 and result cout << factCounter << " = " << factValue << endl; } else { // Recursive case cout << factCounter << " * "; nextCounter = factCounter - 1; nextValue = nextCounter * factValue; /* Your solution goes here */ } } int main() { int userVal; userVal = 5; cout << userVal << "! = "; PrintFactorial(userVal, userVal); return 0; }
Oswaldo J.
Cora M.
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
Watch the video solution with this free unlock.
EMAIL
PASSWORD