Home » Ruby programming » Ruby programs

# 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

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.

Learn PCB Designing: PCB DESIGNING TUTORIAL