Ruby program to print Fibonacci series

Fibonacci program in Ruby: Here, we are going to learn how to print a Fibonacci series in Ruby programming language?
Submitted by Hrithik Chandra Prasad, on August 16, 2019

Fibonacci program in Ruby

The task is to develop a program that prints Fibonacci series in Ruby Programming Language.

Before getting into the logic to build Fibonacci series, let us understand what exactly a Fibonacci series means. Fibonacci series is nothing but a series of numbers in which the current number is the sum of the previous two numbers.

e.g. The Fibonacci series up to 10 is: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55

You can observe that the last number 5 is the sum of 2 and 3 and others are similarly the sum of the previous two numbers.

You can put the above scenario in the code logic with the help of recursive as well as non-recursive approach. In the following program, both methods are mentioned.

Methods used:

  • puts: Used to create an interaction with the user by writing texts on the console.
  • gets: This method is used to take input from the user in the form of string.
  • fib(): This is a user-defined method which is following the recursive approach of finding the Fibonacci series.

Ruby code to print a Fibonacci series

=begin 
Ruby program to print Fibonacci series 
without recursion
=end

first=0
second=1
nextterm=0

puts "Enter the number of terms:-"
n=gets.chomp.to_i

puts "The first #{n} terms of Fibonacci series are:-"
c=1
while(c<=n+1)
	if(c<=1)
		nextterm=c
	else
		puts nextterm
		nextterm=first+second
		first=second
		second=nextterm
	end
	c+=1
end

Output

Enter the number of terms:-
15 
The first 15 terms of Fibonacci series are:- 
1
1
2
3
5
8
13 
21 
34 
55 
89 
144
233
377
610

Method 2:

=begin 
Ruby program to print Fibonacci series with recursion
=end

def fib(n)
	if (n<=2)
		return 1
	else
		return (fib(n-1)+fib(n-2))
	end
end

puts "Enter the number of terms:-"
n=gets.chomp.to_i

puts "The first #{n} terms of fibonnaci series are:-"
for c in 1..n
	puts fib(c)
end

Output

Enter the number of terms:-
15 
The first 15 terms of fibonnaci series are:- 
1
1
2
3
5
8
13 
21 
34 
55 
89 
144
233
377
610

Ruby Basic Programs »




Comments and Discussions!

Load comments ↻





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