Skip to content

Instantly share code, notes, and snippets.

@kehiy
Last active April 3, 2024 10:10
Show Gist options
  • Save kehiy/cb79806aa4174bf366d06a865b2b0aa2 to your computer and use it in GitHub Desktop.
Save kehiy/cb79806aa4174bf366d06a865b2b0aa2 to your computer and use it in GitHub Desktop.
factorial calculation
#include <stdio.h>
int factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial(n -1);
}
int main()
{
printf("%d", factorial(5)); // 5! = 120
return 0;
}
package main
import "fmt"
func facto(n int) int {
if n == 0 {
return 1
}
return n * facto(n - 1)
}
func main() {
f := facto(5)
fmt.Println(f) // 5! = 120
}
(defun factorial (n)
(if (<= n 1)
1
(* n (factorial (- n 1)))))
(factorial 10)
def factorial(n: int):
if n == 0 :
return 1
return n * factorial(n - 1)
print(factorial(5)) # 5! = 120
fn factorial(n: i8) -> i8 {
if n == 0 {
return 1;
}
n * factorial(n - 1)
}
fn main() {
println!("{}", factorial(5)); // 5! = 120
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment