Skip to content

Commit a709f73

Browse files
authored
Add Factorial in V (#4091)
1 parent cdf7572 commit a709f73

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

archive/v/v/factorial.v

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module main
2+
3+
import os
4+
import strconv
5+
6+
fn factorial(n u64) u64 {
7+
return match true {
8+
n <= 1 { 1 }
9+
else { n * factorial(n-1) }
10+
}
11+
}
12+
13+
fn usage() {
14+
println("Usage: please input a non-negative integer")
15+
exit(1)
16+
}
17+
18+
fn main() {
19+
if os.args.len != 2 {
20+
usage()
21+
}
22+
23+
n := strconv.atoi(os.args[1]) or {
24+
usage()
25+
exit(1)
26+
}
27+
if n < 0 {
28+
usage()
29+
}
30+
println(factorial(u64(n)))
31+
}

0 commit comments

Comments
 (0)