Factorial in Ruby

Published on 02 October 2024 (Updated: 02 October 2024)

Welcome to the Factorial in Ruby page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

# return factorial of a non-negative number
if ARGV.empty?
  puts "Usage: please input a non-negative integer"
  exit
else
  begin
    string1 = ARGV[0]
    num = Integer(string1)
    rescue ArgumentError
    puts "Usage: please input a non-negative integer"
    exit
  end
  if num < 0
    puts "Usage: please input a non-negative integer"
    exit    
  end
  i = 1
  factorial = 1
  while i <= num
    factorial = factorial * i
    i += 1
  end
  print(factorial)
end  

Factorial in Ruby was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.