Ruby program to print Fibonacci series

bookmark

=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