Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to Sum a Given Number into Single Digit Using Nested While Loop in Python?

Sum a Given Number into Single Digit Using Nested While Loop in Python

Condition for Sum a Given Number into Single Digit Using Nested While Loop in Python

  • Description: Summing a given number's digits until the result is a single digit can be done by repeatedly adding the digits together.
    This process is known as digit root or repeated digital sum.
Step-by-Step Process
  • Start with the given number: Begin with the number whose digits you need to sum.
  • Use a while loop to check if the number has more than one digit: Check if the number has more than one digit by ensuring it's greater than 9.
  • Sum the digits of the number: In the while loop, extract each digit of the number and sum them together.
  • Repeat the process with the resulting sum: If the result is still more than one digit, repeat the process until a single digit remains.
  • Return the final single-digit result: Once the number becomes a single digit, return the final result.
Sample Code
  • #Sum a given number into single digit
    def sum_to_single_digit(n):
     while n >= 10:
      total_sum = 0
      while n > 0:
       total_sum += n % 10
       n = n // 10
     n = total_sum
     return n
    number = int(input("Enter the number:" ))
    result = sum_to_single_digit(number)
    print(f"The single-digit sum of {number} is {result}")
Screenshots
  • Sum a Given Number into Single Digit Using Nested While Loop