ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.