Find the sum of the digits of an integer using a while loop in python

The provided Python program is designed to find the sum of the digits of an integer using a while loop. The program begins by defining a function called sum_of_digits that takes an integer, number, as its parameter. Within this function, a variable digit_sum is initialized to zero, representing the running sum of the individual digits. The program then enters a while loop that continues as long as the input number is greater than zero. In each iteration, the last digit of the number is extracted using the modulo operator (%) and added to the digit_sum. The last digit is removed from the number by performing integer division (//), ensuring that the loop processes each digit of the original number. After the loop completes, the function returns the calculated digit_sum.


Source code

def sum_of_digits(number):
    # Initialize the sum
    digit_sum = 0
    
    # Use a while loop to iterate through each digit
    while number > 0:
        # Get the last digit
        digit = number % 10
        
        # Add the digit to the sum
        digit_sum += digit
        
        # Remove the last digit from the number
        number //= 10

    # Return the sum of digits
    return digit_sum

# Get an integer input from the user
num = int(input("Enter an integer: "))

# Call the function and print the result
result = sum_of_digits(num)
print("Sum of digits:", result)

Description

  1. Function Definition:
    • The program starts by defining a function named sum_of_digits that takes an integer parameter called number.
  2. Variable Initialization:
    • Inside the function, a variable digit_sum is initialized to zero. This variable will be used to store the running sum of the individual digits.
  3. While Loop:
    • The function enters a while loop, which continues executing as long as the value of the input number (number) is greater than zero.
  4. Digit Extraction:
    • Within the loop, the last digit of the number is obtained using the modulo operator (number % 10). This extracts the rightmost digit.
  5. Sum Update:
    • The extracted digit is added to the digit_sum, accumulating the sum of digits.
  6. Digit Removal:
    • The last digit is removed from the number by performing integer division (number //= 10), ensuring that the loop processes the next digit in the next iteration.
  7. Loop Continuation:
    • Steps 4-6 are repeated until all digits of the original number have been processed.
  8. Return Statement:
    • After the loop completes, the function returns the calculated digit_sum.
  9. User Input:
    • In the main part of the program, the user is prompted to enter an integer using the input function. The entered value is converted to an integer using int().
  10. Function Call:
    • The sum_of_digits function is called with the user-entered integer as an argument, and the result is stored in the variable result.
  11. Result Printing:
    • Finally, the program prints the sum of the digits by displaying the value stored in the result variable.

Screenshot

Output:


Download

By Admin

Leave a Reply

Your email address will not be published. Required fields are marked *