Skip to content

Commit 66cfdfe

Browse files
committed
02_basic_calculator/05_factorial
1 parent fbb4fb1 commit 66cfdfe

File tree

1 file changed

+11
-0
lines changed
  • exercises/02_basic_calculator/05_factorial/src

1 file changed

+11
-0
lines changed

exercises/02_basic_calculator/05_factorial/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@
99
// `factorial(2)` to return `2`, and so on.
1010
//
1111
// Use only what you learned! No loops yet, so you'll have to use recursion!
12+
fn factorial(n: u32) -> u32 {
13+
if n == 0 {
14+
return 1;
15+
}
16+
17+
if n == 1 {
18+
return 1;
19+
}
20+
21+
n * factorial(n - 1)
22+
}
1223

1324
#[cfg(test)]
1425
mod tests {

0 commit comments

Comments
 (0)