0% found this document useful (0 votes)
14 views7 pages

Document 2

The document contains Python programming assignments that include a calculator program, a number analysis program, a date-to-day conversion algorithm, and a grading system based on marks in three subjects. Each program features user input, conditional statements, and error handling. The document emphasizes efficient algorithms and user-friendly outputs.

Uploaded by

23f3000485
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views7 pages

Document 2

The document contains Python programming assignments that include a calculator program, a number analysis program, a date-to-day conversion algorithm, and a grading system based on marks in three subjects. Each program features user input, conditional statements, and error handling. The document emphasizes efficient algorithms and user-friendly outputs.

Uploaded by

23f3000485
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

ASSIGNMENT OF PYTHON PROGRAMMING

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 in ('1', '2', '3', '4'):


num = float(input("Enter a number: "))

if choice == '1':
if num % 2 == 0:
print(f"{num} is even")
else:
print(f"{num} is odd")

elif choice == '2':


if num > 0:
print(f"{num} is positive")
elif num < 0:
print(f"{num} is negative")
else:
print(f"{num} is zero")

elif choice == '3':


print(f"Square of {num} is {num ** 2}")

elif choice == '4':


if num < 0:
print(f"Square root of {num} is not a real
number")
else:
print(f"Square root of {num} is {num **
0.5}")

elif choice == '5':


print("Exiting the program. Goodbye!")
break
else:
print("Invalid choice. Please enter a number
between 1 and 5.")

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.

7)physics = input("Enter marks in Physics (out of 100):


")
chemistry = input("Enter marks in Chemistry (out of
100): ")
mathematics = input("Enter marks in Mathematics (out
of 100): ")
try:
physics = float(physics)
chemistry = float(chemistry)
mathematics = float(mathematics)
if physics < 0 or physics > 100 or chemistry < 0 or
chemistry > 100 or mathematics < 0 or mathematics >
100:
print("Error: Marks should be between 0 and
100.")
else:
total_marks = physics + chemistry + mathematics
percentage = (total_marks / 300) * 100
if percentage >= 90:
grade = "A+"
elif 80 <= percentage < 90:
grade = "A"
elif 70 <= percentage < 80:
grade = "B+"
elif 60 <= percentage < 70:
grade = "B"
elif 40 <= percentage < 60:
grade = "C"
else:
grade = "FAIL"
print(f"Total Marks: {total_marks}/300")
print(f"Percentage: {percentage:.2f}%")
print(f"Grade: {grade}")

except ValueError:
print("Error: Invalid input. Please enter a valid
number.")

You might also like