diff --git a/factorial.py b/factorial.py new file mode 100644 index 0000000..cd70302 --- /dev/null +++ b/factorial.py @@ -0,0 +1,24 @@ +""" +Name:Joe George +Title: Factorial of a number (With & Without Recursion) +Date: 3 December 2024 +""" + +# 1. WITHOUT RECURSION +def factorial(n): + result = 1 + for i in range(1,n+1): + result *= i + return result + +print(factorial(4)) + + +# 2. WITH RECURSION +def recursion_factorial(n): + if n == 1: + return 1 + else: + return n*factorial(n-1) + +print(recursion_factorial(4)) \ No newline at end of file diff --git a/greatest common divisor.py b/greatest common divisor.py new file mode 100644 index 0000000..09f8338 --- /dev/null +++ b/greatest common divisor.py @@ -0,0 +1,15 @@ +''' +Name:Joe George +Title: Greatest Common Divisor. +Date: 3 December 2024 +''' + +# Greatest Common Divisor +def gcd(a,b): + if a % b==0: + return b + else: + return gcd(b,a%b) + +print(gcd(12,16)) + diff --git a/recursive addition.py b/recursive addition.py new file mode 100644 index 0000000..03adfdb --- /dev/null +++ b/recursive addition.py @@ -0,0 +1,14 @@ +""" +Name:Joe George +Title: Addition using recursion +Date: 3 December 2024 +""" + +def add_nums(a, b): + if b == 0: + return a + else: + return add_nums(a+1, b-1) + +print(add_nums(10, 10)) + diff --git a/recursive multiply.py b/recursive multiply.py new file mode 100644 index 0000000..1deae3d --- /dev/null +++ b/recursive multiply.py @@ -0,0 +1,13 @@ +""" +Name:Joe George +Title: Multiplication using recursion +Date: 3 December 2024 +""" + +def multiply(a, b): + if b == 1: + return a + else: + return a + multiply(a, b-1) + +print(multiply(10, 10)) \ No newline at end of file