Skip to content

Commit

Permalink
Add Factorial in Crystal (#4487)
Browse files Browse the repository at this point in the history
  • Loading branch information
HunterYates authored Feb 16, 2025
1 parent 023a87f commit f9c3405
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions archive/c/crystal/factorial.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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

0 comments on commit f9c3405

Please # to comment.