This Python program reads a number from the user and calculates two values:
- The square of the number
- The inverse (reciprocal) of the number
It then prints both results clearly.
# Get input from user
num = float(input("Enter a number: "))
# Calculate square and inverse
square = num ** 2
inverse = 1 / num if num != 0 else "Undefined (division by zero)"
# Display results
print("\n--- Result ---")
print(f"Square: {square}")
print(f"Inverse: {inverse}")
Enter a number: 4
--- Result ---
Square: 16.0
Inverse: 0.25
- The number is converted to float to support decimals
- The square is calculated using exponentiation (** 2)
- The inverse is calculated as 1 / num, with a check to avoid division by zero
- Results are printed with clear labels