Search code examples
rubyfactorial

How to calculate factorial in ruby


QUESTION :

Input:
  4 # number of input 
  1
  2
  4
  3
Output:
  1
  2
  24
  6

CANNOT GET DESIRED OUTPUT

My code:
num  = Integer(gets.chomp)
k = []
for i in 1..num
k[i] = Integer(gets.chomp)
end

k.each do |w|
for i in 1..w
w.to_i = w*i
end
puts w 
end

Solution

  • you can try like:

    input = Integer(gets.chomp) 
    ans = 1
    for i in 1..input
     ans = ans*i
    end
    print(ans)