-6

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
bindi bhatt
  • 178
  • 1
  • 4
  • 15

2 Answers2

1

To get factorial of n

(1..n).inject :*

To take care of zero

(1..n).inject(1, :*)
Santhosh
  • 28,097
  • 9
  • 82
  • 87
0

you can try like:

input = Integer(gets.chomp) 
ans = 1
for i in 1..input
 ans = ans*i
end
print(ans)
SNEH PANDYA
  • 797
  • 7
  • 22