Advertisement
brandblox

python lab (27/02/2024)

Feb 27th, 2024
750
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | None | 0 0
  1. #calculator
  2.  
  3. def addition(num1, num2):
  4.     num1 += num2
  5.     return num1
  6.  
  7. def subtraction(num1, num2):
  8.     num1 -= num2
  9.     return num1
  10.  
  11. def mul(num1, num2):
  12.     num1 *= num2
  13.     return num1
  14.  
  15. def division(num1, num2):
  16.     if num2 != 0:
  17.         num1 /= num2
  18.         return num1
  19.     else:
  20.         return "Cannot divide by zero"
  21.  
  22. def module(num1, num2):
  23.     if num2 != 0:
  24.         num1 %= num2
  25.         return num1
  26.     else:
  27.         return "Cannot perform modulus with zero"
  28.  
  29. def default(num1, num2):
  30.     return "Incorrect day"
  31.  
  32. switcher = {
  33.     1: addition,
  34.     2: subtraction,
  35.     3: mul,
  36.     4: division,
  37.     5: module
  38. }
  39.  
  40. def switch(operation):
  41.     num1 = float(input("Enter the first number: "))
  42.     num2 = float(input("Enter the second number: "))
  43.     return switcher.get(operation, default)(num1, num2)
  44.  
  45. print('''You can perform the following operations:
  46.  
  47. 1. **Addition**
  48. 2. **Subtraction**
  49. 3. **Multiplication**
  50. 4. **Division**
  51. 5. **Modulus (Remainder)''')
  52.  
  53. # Take input from the user
  54. choice = int(input("Select an operation from 1 to 5: "))
  55. print(switch(choice))
  56.  
  57.  
  58.  
  59.  
  60. #Fibonacchi using recursion
  61. def fibonacci(n):
  62.     if n <= 0:
  63.         return "Please enter a positive integer"
  64.     elif n == 1:
  65.         return [0]
  66.     elif n == 2:
  67.         return [0, 1]
  68.     else:
  69.         fib_series = fibonacci(n - 1)
  70.         fib_series.append(fib_series[-1] + fib_series[-2])
  71.         return fib_series
  72.  
  73. n = int(input("Enter the value of 'n' to print Fibonacci series up to n: "))
  74.  
  75. result = fibonacci(n)
  76. print(f"Fibonacci series up to {n}: {result}")
  77.  
  78.  
  79.  
  80. #GCD using recurtion
  81. def gcd_recursive(a, b):
  82.     if b == 0:
  83.         return a
  84.     else:
  85.         return gcd_recursive(b, a % b)
  86.  
  87. # Take input from the user
  88. num1 = int(input("Enter the first number: "))
  89. num2 = int(input("Enter the second number: "))
  90.  
  91. result = gcd_recursive(num1, num2)
  92. print(f"The GCD of {num1} and {num2} is: {result}")
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement