# FLOW006 - Editorial

**URL:** <https://discuss.codechef.com/t/flow006-editorial/120726>\
**Category:** editorial\
**Tags:** digitsum, practice, sum, basic-math\
**Created:** [October 19, 2024, 10:12am UTC](https://discuss.codechef.com/t/flow006-editorial/120726 "2024-10-19T10:12:23Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![aditi\_sahu2212](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@aditi\_sahu2212](https://discuss.codechef.com/u/aditi_sahu2212)\
**Post date:** [October 19, 2024, 10:12am UTC](https://discuss.codechef.com/t/flow006-editorial/120726/1 "2024-10-19T10:12:24Z")

</div>

Problem Link - [Sum of Digits Practice Problem in Basic Math](https://www.codechef.com/practice/course/basic-math/BASICMATH/problems/FLOW006)

### Problem Statement:

You’re given an integer **N**. Write a program to calculate the sum of all the digits of **N**.

### Approach:

For Calculating the sum of digits, we have to extract each digit from the number and then add them up.  
Steps:

- Initialize a variable to store the sum of digits (let’s call it `sum`).
- Use a loop to repeatedly extract the last digit of N using the modulus operator (`% 10`), add this digit to `sum`, and then remove the last digit by dividing N by 10 (`N = N/10`).
- Continue this process until N becomes `0`.

### Complexity:

- **Time Complexity** : The time complexity for calculating the sum of digits for a single number `N` is `O(d)`, where `d` is the number of digits in `N`. In the worst case, since N≤1,000,000, `d` can be at most `7`.
- **Space Complexity** : `O(1)` No extra space is required
