a) Program to determine whether a given year is a leap year
def is_leap_year(year):
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
return True
else:
return False
year = int(input("Enter a year: "))
if is_leap_year(year):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
b) Program to determine the greatest common divisor (GCD) and least
common multiple (LCM) of two integers
import math
def gcd(a, b):
return [Link](a, b)
def lcm(a, b):
return abs(a * b) // [Link](a, b)
num1 = int(input("Enter the first integer: "))
num2 = int(input("Enter the second integer: "))
gcd_value = gcd(num1, num2)
lcm_value = lcm(num1, num2)
print(f"The GCD of {num1} and {num2} is: {gcd_value}")
print(f"The LCM of {num1} and {num2} is: {lcm_value}")
c) Program to create a calculator application
def calculator():
print("Enter an operation in the format: N1 OP N2")
n1 = float(input("Enter the first number (N1): "))
op = input("Enter the operator (one of: +, -, *, /, %, **): ")
n2 = float(input("Enter the second number (N2): "))
if op == '+':
result = n1 + n2
elif op == '-':
result = n1 - n2
elif op == '*':
result = n1 * n2
elif op == '/':
if n2 != 0:
result = n1 / n2
else:
return "Error! Division by zero."
elif op == '%':
result = n1 % n2
elif op == '**':
result = n1 ** n2
else:
return "Invalid operator!"
return f"The result of {n1} {op} {n2} is: {result}"