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
- Function Definition:
- The program starts by defining a function named
sum_of_digitsthat takes an integer parameter callednumber.
- The program starts by defining a function named
- Variable Initialization:
- Inside the function, a variable
digit_sumis initialized to zero. This variable will be used to store the running sum of the individual digits.
- Inside the function, a variable
- While Loop:
- The function enters a
whileloop, which continues executing as long as the value of the input number (number) is greater than zero.
- The function enters a
- Digit Extraction:
- Within the loop, the last digit of the number is obtained using the modulo operator (
number % 10). This extracts the rightmost digit.
- Within the loop, the last digit of the number is obtained using the modulo operator (
- Sum Update:
- The extracted digit is added to the
digit_sum, accumulating the sum of digits.
- The extracted digit is added to the
- 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.
- The last digit is removed from the number by performing integer division (
- Loop Continuation:
- Steps 4-6 are repeated until all digits of the original number have been processed.
- Return Statement:
- After the loop completes, the function returns the calculated
digit_sum.
- After the loop completes, the function returns the calculated
- User Input:
- In the main part of the program, the user is prompted to enter an integer using the
inputfunction. The entered value is converted to an integer usingint().
- In the main part of the program, the user is prompted to enter an integer using the
- Function Call:
- The
sum_of_digitsfunction is called with the user-entered integer as an argument, and the result is stored in the variableresult.
- The
- Result Printing:
- Finally, the program prints the sum of the digits by displaying the value stored in the
resultvariable.
- Finally, the program prints the sum of the digits by displaying the value stored in the
Screenshot

Output:

