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 to 120.

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.

Ruby Basic Programs »




Comments and Discussions!

Load comments ↻





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