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++ (10th 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
- Code in C# Write a method int sum (int, int) with takes 2 parameter as an input to the function and return the sum of these numbers(negative or positive) , to the calling function by using recursion sum (6, 5 ) return 11 sum (-4, 3) return -1 sum (-3, -4 ) return -7arrow_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 recursive function named choose (int n,int k) that will compute and return the value of the binomial coefficient. write it as a C++ function on the computer.arrow_forwardWrite 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_forward
- Solve in C++arrow_forwardin c++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