This Python program reads an integer from the user and calculates two values:
- The square of the number
- The sum of its digits
It then prints both results clearly and concisely.
# Read number from user
num = int(input("Enter a number: "))
# Calculate square
square = num ** 2
# Calculate digit sum
digit_sum = sum(int(digit) for digit in str(abs(num)))
# Display results
print("\n--- Result ---")
print(f"Square: {square}")
print(f"Digit sum: {digit_sum}")
Enter a number: 432123
--- Result ---
Square: 186729129
Digit sum: 15
- The number is read as an integer
- The square is calculated using exponentiation (** 2)
- The digit sum is computed by converting the number to a string, iterating over each digit, and summing them
- abs() ensures the logic works for negative numbers too