Factorial in Crystal

Published on 16 February 2025 (Updated: 16 February 2025)

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

Current Solution

begin
  if ARGV.empty?
    raise "Usage: please input a non-negative integer"
  end

  input = ARGV[0].to_i?

  if input.nil?
    raise "Usage: please input a non-negative integer"
  end

  input = input.not_nil!

  if input >= 0 && input <= 12
    puts factorial(input)
  elsif input > 12
    raise "#{input} is out of the reasonable bounds for calculation."
  else
    raise "Usage: please input a non-negative integer"
  end

rescue e
  puts e.message
end

def factorial(n)
  return 1 if n == 0
  n * factorial(n - 1)
end

Factorial in Crystal 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.