1 . The following variables are used in the program:
- seq variable is of integer type and used to store the user-entered Fibonacci sequence.
- nvariable is of integer type and used as a parameter of the function name findFibonacciNumber.
2. The following functions are used in the program:
- findFibonacciNumber ()function is used to find the Fibonacci number of the user-entered sequence.
- main () function is used to get the user input, call the function, and display the Fibonacci number returned by the function.
Program description:
The main purpose of the program is to create a function named findFibonacciNumber . This function get the sequence of the Fibonacci series as parameter, then calculate the Fibonacci number forsequence, and return the Fibonacci number.
Explanation of Solution
Program:
//including essential header file #include <iostream> //using standard namespace using namespace std; //function to find the Fibonacci number of the given sequence int findFibonacciNumber(int n) { //when the fibonacci sequence is equal to 0 or 1 if (n == 0 || n == 1) { //return 1 return 1; } //call the function recursively to find the fibonacci number return findFibonacciNumber(n-1) + findFibonacciNumber(n-2); } //main function int main() { //variable declaration to store the sequence of Fibonacci number int seq; //prompt the user to enter the fibonacci sequence cout<<"Enter the fibonacci sequence: "; //get the value from the user cin>>seq; //call the function to find and display the fibonacci number of the given sequence cout<<"The fibonacci number of sequence "<<seq<<" is "<<findFibonacciNumber(seq); }
Explanation:
In the above code, a recursive function named findFibonacciNumber is used to find the Fibonacci number of the user-entered sequence. It has a parameter named n of integer type. It gets the sequence of the Fibonacci series from the main () function and stored in the parameter named n. When the value of the Fibonacci sequence is 0 or 1 then this function returns 1. For the other values this function return the Fibonacci number for the given sequence by adding the immediate last two Fibonacci number by calling itself recursivelyand decreasing the value of the parameter by 1 (to get the immediate last value) and 2 (to get the second last value).
In the main function, create a variable named seq of integer type to store the user-entered Fibonacci sequence. Prompt the user to enter the Fibonacci sequence. Store the user-entered Fibonacci sequence in the variable name seq. Call the function named findFibonacciNumber with seq as parameter. Display the function returned value.
Sample output:
Want to see more full solutions like this?
Chapter 13 Solutions
ABSOLUTE C++ -TEXT
- Matlab Questionarrow_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_forwardIn C programming Mathematically, given a function f, we recursively define fk(n) as follows: if k = 1, f1(n) = f(n). Otherwise, for k > 1, fk(n) = f(fk-1(n)). Assume that there is an existing function f, which takes in a single integer and returns an integer. Write a recursive function fcomp, which takes in both n and k (k > 0), and returns fk(n). int f(int n);int fcomp(int n, int k){arrow_forward
- Write an iterative function that determines the number of even elements in an array a of integers of size n. The function should return the number of elements that are even in array a of size n. Propose an appropriate prototype for your function and then write its code. Write a recursive function to solve the above problem. Propose an appropriate prototype for your function and then write its code.arrow_forwardPerform in C#arrow_forwardA recursive function is a function defined in terms of itself via self-referential expressions. This means that the function will continue to call itself and repeat its behavior until some condition is met to return a result. a- Write a python recursive function prod that takes x as an argument, and returns the result where, result=1*1/2*1/3*….*1/n b- Include a screenshot that shows a python program that uses the above function and prints the rounded result to three decimal places after prompting the user to enter a number, x. Use x=3. N.B: The code should be included in your answer.arrow_forward
- In Kotlin A. Write a function lengthGreaterThan() with an expression body that takes a String s and an Int n and returns true if the String is at least n characters long. Then revise the function so that it can accept null values. If it receives null as the parameter, it should return false. B. Write a recursive function with an expression body that takes a list of nullable Strings and and Int an returns a list of just the Strings from the original list for which the function you wrote in the first part returns true C. Get the same result as in part B by using filter()arrow_forwardMULTIPLE FUNCTIONS AND RECURSIVE FUNCTIONS .arrow_forwardWrite a program that performs the following functionalities:1. Fibonacci: a recursive function that computes the fibonacci series, which is defined as followsfibonacci (n) = fibonacci(n-2) + fibonacci(n-1)fibonacci(0) = 0fibonacci(1) = 1 So the fibonacci looks like: 0 1 1 2 3 5 8 13 21 34 …. Therefore, fibonacci(4) = 3, fibonacci(5) = 5, fibonacci(7)=13 2. Sum: a recursive function that computes the sum of integers 1, 2, 3, …., n for a given number n. So Sum(6) should return 1 + 2 + 3 + 4 + 5 + 6 , i.e. 21.sum(n) = n + sum(n-1) 3. Largest: a recursive function that computes the largest value for an integer array of positiveand negative values. For example, for the array below, the function largest should return 22,which is the largest value in the array. You can assume there are no more 20 integers in thearray. Think of how to formulate the recurrence relation in this problem yourself. 4. The 4th problem mimics the situation where eagles flying in the sky can be spotted and…arrow_forward
- Question 1. Consider the following difference equation: With ro := 130, set rn := 0.0001 - 0,0194r1 + 1.6909r-1 (n = 1, 2, 3, ...). Question 1.1. Implement a non-recursive function in Python 3 according to the following specification. Include your implementation in your submission. Function specification: Function name: Input parameters: n: a non -negative integer Return The value of r. Question 1.2. Use matplotlib to make a scatter plot of the the first 21 elements in the solution of the difference equation. That is, make a scatter plot of the dataset {(J.r,) : 1 = 0,1, 2, 3,..., 20). Using the "set-ylim" function on your matplotlib axes, set the bounds of the y-axis to be the interval (0,200].arrow_forwardIn programming, a recursive function calls itself. The classical example is factorial(n), which can be defined recursively as n*factorial(n-1). Nonethessless, it is important to take note that a recursive function should have a terminating condition (or base case), in the case of factorial, factorial(0)=1. Hence, the full definition is: factorial(n) = 1, for n = 0 factorial(n) = n * factorial(n-1), for all n > 1 For example, suppose n = 5: // Recursive call factorial(5) = 5 * factorial(4) factorial(4) = 4 * factorial(3) factorial(3) = 3 * factorial(2) factorial(2) = 2 * factorial(1) factorial(1) = 1 * factorial(0) factorial(0) = 1 // Base case // Unwinding factorial(1) = 1 * 1 = 1 factorial(2) = 2 * 1 = 2 factorial(3) = 3 * 2 = 6 factorial(4) = 4 * 6 = 24 factorial(5) = 5 * 24 = 120 (DONE) Exercise (Factorial) (Recursive): Write a recursive method called factorial() to compute the factorial of the given integer. public static int factorial(int n) The recursive algorithm is:…arrow_forward*19. A recursive function f (x), is defined as follows: if (x>100) return (x-10) else return (f (f (x+11) ) ) For which of the following values of x, (a) 100 (b) 91 f(x) (c) 1 = 91? (d) 101arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning