HomeComputer SciencePracticalClass 11Arithmetic Operations

Basic Arithmetic Operations Calculator

AIM

To write a Python program that performs basic arithmetic operations (addition, subtraction, multiplication, division, and modulus) on two numbers entered by the user.

ALGORITHM

  1. Start
  2. Take two numbers as input from the user
  3. Perform addition and display the result
  4. Perform subtraction and display the result
  5. Perform multiplication and display the result
  6. Perform division and display the result (check for division by zero)
  7. Perform modulus operation and display the result
  8. Perform floor division and display the result
  9. Perform exponentiation and display the result
  10. Stop

PROGRAM

# Python program for basic arithmetic operations

print("Basic Arithmetic Operations Calculator")
print("=" * 45)

# Taking input from the user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

print("\n" + "=" * 45)
print("ARITHMETIC OPERATIONS RESULTS")
print("=" * 45)

# Addition
addition = num1 + num2
print(f"Addition: {num1} + {num2} = {addition}")

# Subtraction
subtraction = num1 - num2
print(f"Subtraction: {num1} - {num2} = {subtraction}")

# Multiplication
multiplication = num1 * num2
print(f"Multiplication: {num1} × {num2} = {multiplication}")

# Division (with zero division check)
if num2 != 0:
    division = num1 / num2
    print(f"Division: {num1} ÷ {num2} = {division:.2f}")
else:
    print("Division: Cannot divide by zero!")

# Modulus (remainder)
if num2 != 0:
    modulus = num1 % num2
    print(f"Modulus: {num1} % {num2} = {modulus}")
else:
    print("Modulus: Cannot find remainder when dividing by zero!")

# Floor Division
if num2 != 0:
    floor_division = num1 // num2
    print(f"Floor Division: {num1} // {num2} = {floor_division}")
else:
    print("Floor Division: Cannot divide by zero!")

# Exponentiation
exponentiation = num1 ** num2
print(f"Exponentiation: {num1} ^ {num2} = {exponentiation}")

print("=" * 45)

# Additional calculations
print("\nADDITIONAL INFORMATION:")
print("-" * 25)
print(f"Absolute difference: |{num1} - {num2}| = {abs(num1 - num2)}")
print(f"Average: ({num1} + {num2}) / 2 = {(num1 + num2) / 2}")
print(f"Maximum: max({num1}, {num2}) = {max(num1, num2)}")
print(f"Minimum: min({num1}, {num2}) = {min(num1, num2)}")

OUTPUT

Basic Arithmetic Operations Calculator

=============================================

Enter first number: 15

Enter second number: 4

=============================================

ARITHMETIC OPERATIONS RESULTS

=============================================

Addition: 15.0 + 4.0 = 19.0

Subtraction: 15.0 - 4.0 = 11.0

Multiplication: 15.0 × 4.0 = 60.0

Division: 15.0 ÷ 4.0 = 3.75

Modulus: 15.0 % 4.0 = 3.0

Floor Division: 15.0 // 4.0 = 3.0

Exponentiation: 15.0 ^ 4.0 = 50625.0

=============================================

ADDITIONAL INFORMATION:

-------------------------

Absolute difference: |15.0 - 4.0| = 11.0

Average: (15.0 + 4.0) / 2 = 9.5

Maximum: max(15.0, 4.0) = 15.0

Minimum: min(15.0, 4.0) = 4.0

CONCLUSION

Thus, the given program was successfully executed and the output was verified as per the expected result.

VIVA QUESTIONS

  1. What are arithmetic operators in Python?

    Arithmetic operators are symbols used to perform mathematical operations: +, -, *, /, %, //, and **.

  2. What is the difference between / and // operators?

    / performs true division returning a float, while // performs floor division returning the quotient without the decimal part.

  3. What does the modulus (%) operator do?

    The modulus operator returns the remainder after division. For example, 15 % 4 = 3.

  4. Why do we check for division by zero?

    Division by zero is mathematically undefined and causes a ZeroDivisionError in Python, so we check to prevent program crashes.

  5. What is the exponentiation operator?

    The ** operator performs exponentiation, raising the first number to the power of the second number.

  6. What is operator precedence?

    Operator precedence determines the order of operations. In Python: ** > *, /, //, % > +, -. Parentheses have highest precedence.

  7. What does the abs() function do?

    The abs() function returns the absolute value of a number, which is always positive or zero.

  8. How do you format floating-point numbers in output?

    You can use f-strings with format specifiers like {value:.2f} to display 2 decimal places.