Home » Ruby programming » Ruby programs

# Ruby program to find factorial of a given number

**Finding factorial number in Ruby**: Here, we are going to learn **how to find factorial of a given number in Ruby programming language?**

Submitted by Hrithik Chandra Prasad, on August 07, 2019

## Finding factorial

Given/Input an integer number and find the factorial in Ruby.

We can find the factorial of a number n by multiplying it with its predecessors up to 1. For example, if we have to find factorial of 5, the equation will look like:

5! = 5*4*3*2*1, which will be equal to120.

For implementing the above logic, we can put the variable num in the Boolean condition and after initialising a variable fact, we can simply multiply fact with a variable i which is initialised by 1. Increase i with every iteration and the loop will be terminated when i will become equal to num.

**Methods used:**

**puts**: put string method is used for printing message.**gets**: get string method is used for taking input from the user.**.to_i**: It is converting user inputted string into integer value type.

**Operators used:**

**==**: Equal to operator. Used to check the equality of left hand value with the right hand one.**=**: Assignment operator. Used to assign a value to the variables.**<=**: Less than or equal to operator. Basically used for comparison.*****: Multiplication Arithmetic Operator used for the multiplication.**+**: Addition Arithmetic Operator. Used for adding two variables.

**Variables used:**

**num**: Used to store the value given by the user.**fact**: Used to store the final factorial.**i**: It is acting as a loop variable.

### Ruby code to find the factorial of a given number

=begin Ruby program to find the factorial of a given number. =end puts "Enter the number:" num=gets.chomp.to_i fact=1 if (num==0) puts "Error! Could not find the factorial of one" else i=1 while(i<=num) fact=fact*i i+=1 end end puts "factorial of #{num} is #{fact}"

**Output**

Run 1: Enter the number: 5 factorial of 5 is 120 Run 2: Enter the number: 7 factorial of 7 is 5040

This code takes a number as input and return another number that is the factorial of this number, and prints this factorial to the output screen.

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