Skip to content

Commit

Permalink
Add Factorial in V (#4091)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaamkiya authored Dec 16, 2024
1 parent cdf7572 commit a709f73
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions archive/v/v/factorial.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module main

import os
import strconv

fn factorial(n u64) u64 {
return match true {
n <= 1 { 1 }
else { n * factorial(n-1) }
}
}

fn usage() {
println("Usage: please input a non-negative integer")
exit(1)
}

fn main() {
if os.args.len != 2 {
usage()
}

n := strconv.atoi(os.args[1]) or {
usage()
exit(1)
}
if n < 0 {
usage()
}
println(factorial(u64(n)))
}

0 comments on commit a709f73

Please sign in to comment.