Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #calculator
- def addition(num1, num2):
- num1 += num2
- return num1
- def subtraction(num1, num2):
- num1 -= num2
- return num1
- def mul(num1, num2):
- num1 *= num2
- return num1
- def division(num1, num2):
- if num2 != 0:
- num1 /= num2
- return num1
- else:
- return "Cannot divide by zero"
- def module(num1, num2):
- if num2 != 0:
- num1 %= num2
- return num1
- else:
- return "Cannot perform modulus with zero"
- def default(num1, num2):
- return "Incorrect day"
- switcher = {
- 1: addition,
- 2: subtraction,
- 3: mul,
- 4: division,
- 5: module
- }
- def switch(operation):
- num1 = float(input("Enter the first number: "))
- num2 = float(input("Enter the second number: "))
- return switcher.get(operation, default)(num1, num2)
- print('''You can perform the following operations:
- 1. **Addition**
- 2. **Subtraction**
- 3. **Multiplication**
- 4. **Division**
- 5. **Modulus (Remainder)''')
- # Take input from the user
- choice = int(input("Select an operation from 1 to 5: "))
- print(switch(choice))
- #Fibonacchi using recursion
- def fibonacci(n):
- if n <= 0:
- return "Please enter a positive integer"
- elif n == 1:
- return [0]
- elif n == 2:
- return [0, 1]
- else:
- fib_series = fibonacci(n - 1)
- fib_series.append(fib_series[-1] + fib_series[-2])
- return fib_series
- n = int(input("Enter the value of 'n' to print Fibonacci series up to n: "))
- result = fibonacci(n)
- print(f"Fibonacci series up to {n}: {result}")
- #GCD using recurtion
- def gcd_recursive(a, b):
- if b == 0:
- return a
- else:
- return gcd_recursive(b, a % b)
- # Take input from the user
- num1 = int(input("Enter the first number: "))
- num2 = int(input("Enter the second number: "))
- result = gcd_recursive(num1, num2)
- print(f"The GCD of {num1} and {num2} is: {result}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement