askvity

How do you add double digit numbers in Python?

Published in Python Programming 1 min read

Here's how you can add the digits of a double-digit number in Python:

def add_double_digit(number):
  """Adds the digits of a double-digit number.

  Args:
    number: An integer between 10 and 99 (inclusive).

  Returns:
    The sum of the digits, or an error message if the input is invalid.
  """
  if 10 <= number <= 99:
    number_str = str(number)
    digit1 = int(number_str[0])
    digit2 = int(number_str[1])
    return digit1 + digit2
  else:
    return "Input must be a double-digit number (10-99)."

# Example usage:
num = 42
sum_of_digits = add_double_digit(num)
print(f"The sum of the digits of {num} is: {sum_of_digits}")  # Output: The sum of the digits of 42 is: 6

num = 77
sum_of_digits = add_double_digit(num)
print(f"The sum of the digits of {num} is: {sum_of_digits}") # Output: The sum of the digits of 77 is: 14

num = 5
sum_of_digits = add_double_digit(num)
print(sum_of_digits) # Output: Input must be a double-digit number (10-99).

Explanation:

  1. Input Validation: The add_double_digit function first checks if the input number is within the range of 10 to 99. This ensures it's a double-digit number. If not, it returns an appropriate error message.

  2. Convert to String: The integer is converted into a string using str(number). This allows us to access individual digits by their index.

  3. Extract Digits: We extract the first and second digits using string indexing: number_str[0] and number_str[1]. These are still string representations of the digits.

  4. Convert to Integers: The extracted digits (which are strings) are converted back to integers using int(). This is necessary for performing arithmetic.

  5. Calculate Sum: Finally, the two integer digits are added together, and the sum is returned.

Alternative (Mathematical) Approach:

def add_double_digit_math(number):
  """Adds the digits of a double-digit number using mathematical operations.

  Args:
    number: An integer between 10 and 99 (inclusive).

  Returns:
    The sum of the digits, or an error message if the input is invalid.
  """
  if 10 <= number <= 99:
    digit1 = number // 10  # Integer division to get the tens digit
    digit2 = number % 10   # Modulo operator to get the ones digit
    return digit1 + digit2
  else:
    return "Input must be a double-digit number (10-99)."

# Example Usage
num = 23
sum_of_digits = add_double_digit_math(num)
print(f"The sum of the digits of {num} is: {sum_of_digits}") # Output: The sum of the digits of 23 is: 5

This approach uses integer division (//) to extract the tens digit and the modulo operator (%) to extract the ones digit. It avoids string conversions.

In summary, you can add the digits of a double-digit number in Python by converting it to a string and accessing its characters or using mathematical operations like integer division and the modulo operator. The function should validate the input to ensure it is actually a double-digit number.

Related Articles