Please Login in order to post a comment
def factorial(n) total = n for i in 1...n do total *= i end yield total end n = gets.to_i factorial(n) do |num| puts num end
def factorial(n) yield(n) end n = gets.to_i puts factorial(n) { (1..n).reduce(1, :*) }
def factorial(n, i) yield(n, i) end n = gets.to_i i = n - 1 puts factorial(n, i) { while i != 0 n = n * i i -= 1 end n }
Here is blocks problem solution - https://www.gyangav.com/2022/10/hackerrank-ruby-blocks-problem-solution.html
def factorial yield end n = gets.to_i factorial do puts (1..n).reduce(:*) end
Seems like cookies are disabled on this browser, please enable them to open this website
Here is blocks problem solution - https://www.gyangav.com/2022/10/hackerrank-ruby-blocks-problem-solution.html