Q:

Ruby program to print the Fibonacci series using recursion

belongs to collection: Ruby User-defined Functions Programs

0

In this program, we will create a recursive function to print the Fibonacci series based on specified terms using recursion.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

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 ab 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

total answers (1)

Ruby User-defined Functions Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Ruby program to calculate the power of a given num... >>
<< Ruby program to calculate the factorial of given n...