The source code to print the Fibonacci series using recursion is given below. The given program is compiled and executed successfully.
# Ruby program to print the
# Fibonacci series using recursion
def printFibonacci(a,b,term)
if term > 0
sum = a + b;
print sum, " ";
a = b;
b = sum;
printFibonacci(a, b, term-1);
end
end
a=0;
b=1;
printFibonacci(a, b, 6);
Output:
1 2 3 5 8 13
Explanation:
In the above program, we created a recursive function printFibonacci(). Here, we created two integer variables a, b initialized with 0, 1 respectively. Then we called the printFibonacci() function to print the Fibonacci series for the specified number of terms.
Program/Source Code:
The source code to print the Fibonacci series using recursion is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we created a recursive function printFibonacci(). Here, we created two integer variables a, b initialized with 0, 1 respectively. Then we called the printFibonacci() function to print the Fibonacci series for the specified number of terms.
need an explanation for this answer? contact us directly to get an explanation for this answer