Question

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
Was it helpful?

Solution 2

you can try like:

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

OTHER TIPS

To get factorial of n

(1..n).inject :*

To take care of zero

(1..n).inject(1, :*)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top