Sure, here is a Python program to calculate the sum of digits in a given number:
Python
def sum_of_digits(number):
"""
Calculates the sum of digits in a given number.
Args:
number: The number to calculate the sum of digits for.
Returns:
The sum of the digits in the number.
"""
sum_of_digits = 0
while number > 0:
digit = number % 10
sum_of_digits += digit
number //= 10
return sum_of_digits
if __name__ == "__main__":
number = 12345
sum_of_digits = sum_of_digits(number)
print(sum_of_digits)
This program works by first defining a function called sum_of_digits(). This function takes a number as input and returns the sum of the digits in the number. The function works by first initializing a variable called
sum_of_digits to 0. Then, it enters a while loop that continues as long as the number is greater than 0. Inside the while loop, the function gets the rightmost digit of the number and adds it to the
sum_of_digits variable. Then, the function divides the number by 10 to remove the rightmost digit. The while loop continues until the number is 0. Finally, the function returns the
sum_of_digits variable.
The main function of the program simply gets the number from the user and then calls the
sum_of_digits() function to calculate the sum of the digits. The main function then prints the sum of the digits to the console.
To run the program, you can save it as a Python file and then run it from the command line. For example, if you save the program as
sum_of_digits.py, you can run it by typing the following command into the command line:
Code snippet
python sum_of_digits.py
This will print the sum of the digits to the console.
Here is an example of the output of the program:
Code snippet
$ python sum_of_digits.py
15
As you can see, the output of the program is the sum of the digits in the number 12345, which is 15.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, here is a Python program to calculate the sum of digits in a given number:
Python
This program works by first defining a function called
sum_of_digits(). This function takes a number as input and returns the sum of the digits in the number. The function works by first initializing a variable calledsum_of_digitsto 0. Then, it enters a while loop that continues as long as the number is greater than 0. Inside the while loop, the function gets the rightmost digit of the number and adds it to thesum_of_digitsvariable. Then, the function divides the number by 10 to remove the rightmost digit. The while loop continues until the number is 0. Finally, the function returns thesum_of_digitsvariable.The main function of the program simply gets the number from the user and then calls the
sum_of_digits()function to calculate the sum of the digits. The main function then prints the sum of the digits to the console.To run the program, you can save it as a Python file and then run it from the command line. For example, if you save the program as
sum_of_digits.py, you can run it by typing the following command into the command line:Code snippet
This will print the sum of the digits to the console.
Here is an example of the output of the program:
Code snippet
As you can see, the output of the program is the sum of the digits in the number 12345, which is 15.