Explanation of Solution
Recursive function for nth Fibonacci numbers:
The recursive function for nth Fibonacci number is shown below:
/* Function definition for compute nth Fibonacci number */
int recursiveFibFunction(int n)
{
/* If "n" is less than or equal to "1", then */
if (n <= 1)
//Return value of "n"
return n;
/* Otherwise Recursively call the function "recursiveFibFunction" */
return recursiveFibFunction(n-1) + recursiveFibFunction(n-2);
}
Explanation:
The above function is used to compute the nth Fibonacci numbers.
- In this function, first check the value of “n”. If the value of “n” is less than or equal to “1” that is value of “n” is either “1” or “0”, then returns the given “n” value.
- Otherwise, recursively call the function “recursiveFibFunction”.
Complete executable code:
The complete code is implemented for Fibonacci number is shown below:
//Header file
#include<iostream>
//For standard input and output
using namespace std;
//Function declaration for "recursiveFibFunction" function
int recursiveFibFunction(int n);
//Main function
int main ()
{
//Initializes the number to "8"
int number = 8;
/* Dispay fibonacci number by calling the function "recursiveFibFunction" */
cout << number << "th Fibonacci number is: "<< recursiveFibFunction(number) << endl;
return 0;
}
/* Function definition for compute nth Fibonacci number */
int recursiveFibFunction(int n)
{
/* If "n" is less than or equal to "1", then */
if (n <= 1)
//Return value of "n"
return n;
/* Otherwise Recursively call the function "recursiveFibFunction" */
return recursiveFibFunction(n-1) + recursiveFibFunction(n-2);
}
8th Fibonacci number is: 21
Want to see more full solutions like this?
Chapter 14 Solutions
Problem Solving with C++ (9th Edition)
- Write a function which takes two integer parameters for values to be added together and returns the result by value. The function may not print anything or read anything directly from the user (i.e. no cin/cout in the function). Assume that the values passed to the function will not be negative, but could be 0 or positive, and will both be integers. The function must implement addition recursively, and cannot use the standalone + operator (only ++) or call any other functions.arrow_forwardin c++arrow_forwardFibonacci numbers are a sequence of integers, starting with 1, where the value of each number is the sum of the two previous numbers, e.g. 1, 1, 2, 3, 5, 8, etc. Write a function called fibonacci that takes a parameter, n, which contains an integer value, and have it return the nth Fibonacci number. (There are two ways to do this: one with recursion, and one without.)arrow_forward
- Question 2: Implementing a Recursive Function .Write recursive function, recursionprob(n), which takes a positive number as its argument and returns the output as shown below. The solution should clearly write the steps as shown in an example in slide number 59 and slide number 60 in lecture slides. After writing the steps, trace the function for “recursiveprob(5)” as shown in an example slide number 61. Function Output: >> recursionprob(1) 1 >> recursionprob(2) 1 4 >> recursionprob(3) 1 4 9 >>recrusionprob(4) 1 4 9 16arrow_forwardWrite a recursive function called draw_triangle() that outputs lines of '*' to form a right side up isosceles triangle. Function draw_triangle() has one parameter, an integer representing the base length of the triangle. Assume the base length is always odd and less than 20. Output 9 spaces before the first '*' on the first line for correct formatting. Hint: The number of '*' increases by 2 for every line drawn. Ex: If the input of the program is: 3 the function draw_triangle() outputs: * *** Ex: If the input of the program is: 19 the function draw_triangle() outputs: * *** ***** ******* ********* *********** ************* *************** ***************** ******************* Note: No space is output before the first '*' on the last line when the base length is 19. if __name__ == '__main__': base_length = int(input()) draw_triangle(base_length)arrow_forwardWrite a recursive function that takes as a parameter a nonnegative integer and generates the following pattern of stars. If the nonnegative integer is 4, the pattern generated is as follows: **** *** ** * * ** *** **** Also, write a program that prompts the user to enter the number of lines in the pattern and uses the recursive function to generate the pattern. For example, specifying 4 as the number of lines generates the preceding pattern.arrow_forward
- Write a program that asks a number “N” from user. Write a function that takes this number “N”, then returns the sum of numbers starting from N to 1. Use this function in your program and show the results. Be careful, your function should be recursive! I need code on C programmingarrow_forwardpython codearrow_forwardThe function drawFractalLine is recursive. Write a script that draws the Koch snowflake. Define a function main that will draw a Koch snowflake with the following parameters when the program is run: Width = 200 Height = 200 Size = 150 Level = 4arrow_forward
- For any part that requires recursion, if you do not create a recursive function, that will result in a on that part.arrow_forwardIn Kotlin, Write and test a recursive function called harm with an expression body that takes an Int argument n and calculates the sum of 1/1 ... 1/n. For example, harm(3) should return about 1.833333333. You do not need to worry about integer division.arrow_forwardProblem: Recursive Power Method Design a python function that uses recursion to raise a number to a power. The function should accept two arguments: the number to be raised, and the exponent. Assume the exponent is a nonnegative integer. Write the main() function to input the required parameters as shown in thesample input/output. Sample Output:Average number of words per line: 26.0Enter a number: 2Enter a positive whole number between 1 and 100: 102.0 raised to the power of 10 is 1,024.00arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning