Ruby program to print the Fibonacci series using recursion

Ruby Example: Write a program to print the Fibonacci series using recursion.
Submitted by Nidhi, on December 24, 2021

Problem Solution:

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

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 a, b initialized with 0, 1 respectively. Then we called the printFibonacci() function to print the Fibonacci series for the specified number of terms.

Ruby User-defined Functions Programs »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.