---
title: "Write a python program to calculate the sum of digits in a given number."  
description: "Write a python program to calculate the sum of digits in a given number."  
author: "Steilla Mitchel"  
published: 2023-06-18  
updated: 2023-06-19  
canonical: https://www.mindstick.com/forum/158772/write-a-python-program-to-calculate-the-sum-of-digits-in-a-given-number  
category: "python"  
tags: ["python", "programs"]  
reading_time: 2 minutes  

---

# Write a python program to calculate the sum of digits in a given number.

Implement a [program to calculate](https://www.mindstick.com/forum/158809/implement-a-rust-program-to-calculate-the-sum-of-digits-in-a-given-number) the [sum of digits](https://www.mindstick.com/forum/157685/find-the-sum-of-digits-of-a-number-in-pl-sql) in a given number.

## Replies

### Reply by Aryan Kumar

Sure, here is a Python [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) to calculate the [sum](https://www.mindstick.com/forum/159332/how-can-i-use-sum-in-linq) of digits in a given number:

Python

```plaintext
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

```plaintext
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

```plaintext
$ 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.


---

Original Source: https://www.mindstick.com/forum/158772/write-a-python-program-to-calculate-the-sum-of-digits-in-a-given-number

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
