Document 2
Document 2
UID:-24BCS12677(NAINA CHAUDHARY)
3) print(“
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
print("5. Modulo Operation")
print("6. Exit")
while True:
choice = input("Enter your choice (1-6): ")
if choice in ('1', '2', '3', '4', '5'):
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
print(num1, "+", num2, "=", num1 + num2)
elif choice == '2':
print(num1, "-", num2, "=", num1 - num2)
elif choice == '3':
print(num1, "*", num2, "=", num1 * num2)
elif choice == '4':
if num2 != 0:
print(num1, "/", num2, "=", num1 / num2)
else:
print("Error: Division by zero is not allowed")
elif choice == '5':
if num2 != 0:
print(num1, "%", num2, "=", num1 % num2)
else:
print("Error: Division by zero is not
allowed")
elif choice == '6':
print("Exiting the calculator. Goodbye!")
break
else:
print("Invalid choice. Please enter a
number between 1 and 6.")
2) print("Menu:")
print("1. Check if a number is even or odd")
print("2. Check if a number is positive or negative")
print("3. Print the square of a number")
print("4. Print the square root of a number")
print("5. Exit")
while True:
choice = input("Enter your choice (1-5): ")
if choice == '1':
if num % 2 == 0:
print(f"{num} is even")
else:
print(f"{num} is odd")
4) Algorithm:
1. Input: Day (1-31), Month (1-12), Year (e.g., 2022)
2. Calculate: day_of_week = (day + (13*(month+1))/5
+ year + year/4 - year/100 + year/400) % 7
3. Determine Day: Use the result day_of_week to
determine the day of the week:
- day_of_week = 0 => Sunday
- day_of_week = 1 => Monday
- day_of_week = 2 => Tuesday
- day_of_week = 3 => Wednesday
- day_of_week = 4 => Thursday
- day_of_week = 5 => Friday
- day_of_week = 6 => Saturday
Python Program:
day = int(input("Enter day (1-31): "))
month = int(input("Enter month (1-12): "))
year = int(input("Enter year: "))
day_of_week = (day + (13*(month+1))//5 + year +
year//4 - year//100 + year//400) % 7
days = ["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"]
print(f"The day of the week is: {days[day_of_week]}")
This program takes user input for a date and calculates
the corresponding day of the week using the algorithm.
Efficiency Justification:
This approach is efficient because:
1. Simple Calculation: The algorithm involves a single
calculation, making it fast and efficient.
2. Minimal Decision-Making: The algorithm eliminates
the need for multiple conditional statements, reducing
decision-making time.
3. Scalability: This algorithm can handle dates across
various centuries, making it a scalable solution.
except ValueError:
print("Error: Invalid input. Please enter a valid
number.")