Questions on Recursion
• The Code implementing all problems (1 – 3)
should be in 1 Program.
• DO NOT CREATE SEPARATE FILES
Question 1
Consider the following recursive definition
Fun(n) = 1, for n = 0;
Fun(n) = 3Fun(n-1) + 2, for n ≥ 1;
• Write a C++ function definition to implement the above recursive definition.
• Identify the base case and the general case
• Test your program for Fun(-5), Fun(3), Fun(1), Fun(15), and for each you should
be able to trace your program
Question 2
Write a recursive function that accepts a number as its
argument and returns the sum of all digits from 1 to the
entered number
Eg if the argument is 5, the function should return the
sum of 5 + 4 + 3 + 2 + 1 which is 15
Step 1: Define your base case and general case
Step 2: write a function definition to implement the cases in
step 1
Question 3
Write a C++ program to implement a
recursive function to calculate the power of
a number.
Step 1: Define your base case and general
case
Step 2: write a function definition to
implement the cases in step 1