Learning coding means GreatToCode Be more than a Coder ! Greattocode , Join GreatToCode Community,1000+ Students Trusted On Us .If You want to learn coding, Then GreatToCode Help You.No matter what It Takes !


CODE YOUR WAY TO A MORE FULFILLING And HIGHER PAYING CAREER IN TECH, START CODING FOR FREE Camp With GreatToCode - Join the Thousands learning to code with GreatToCode
Interactive online coding classes for at-home learning with GreatToCode . Try ₹Free Per Month Coding Classes With The Top Teachers . Finding the Sum of Digits in a Number using Python

Finding the Sum of Digits in a Number using Python

Become More Then A coder | Learn & Start Coding Now.

Finding the Sum of Digits in a Number using Python


Introduction:

Calculating the sum of digits in a number is a common programming task that involves extracting individual digits from a given number and then adding them together. In this article, we'll explore a simple Python program to find the sum of digits in a number and provide a detailed explanation of the code.

### Python Program to Find the Sum of Digits:

```python
def sum_of_digits(number):
    total_sum = 0

    while number > 0:
        digit = number % 10
        total_sum += digit
        number = number // 10

    return total_sum

# Example: Finding the sum of digits
input_number = int(input("Enter a number: "))
result = sum_of_digits(input_number)
print("Sum of digits:", result)
```

### Explanation:

#### 1. Function Definition (`sum_of_digits`):

   - The `sum_of_digits` function takes an integer `number` as an argument and returns the sum of its digits.

#### 2. Initialization:

   - `total_sum` is initialized to 0, which will store the sum of digits.
   - The `while` loop runs as long as the original number (`number`) is greater than 0.

#### 3. Extracting and Summing Digits:

   - Inside the loop:
     - `digit` is the last digit of the original number (`number % 10`).
     - The current digit is added to the `total_sum`.
     - The last digit is removed from the original number (`number // 10`).

#### 4. Returning the Result:

   - Once the loop completes, the sum of digits is returned.

#### 5. User Input and Output:

   - The user is prompted to enter a number using `input()`.
   - The entered number is converted to an integer using `int()`.
   - The `sum_of_digits` function is called with the input number, and the result is printed.

### Sample Output:

```
Enter a number: 12345
Sum of digits: 15
```

### Conclusion:

Finding the sum of digits in a number using Python involves a simple algorithm where each digit is extracted and added together. The provided program demonstrates an efficient way to achieve this using a `while` loop. This foundational knowledge can be applied to various programming scenarios, and understanding these basic operations is crucial for developing more complex algorithms. Feel free to experiment with different inputs and adapt the program to suit specific requirements in your projects. Happy coding!

Post a Comment

0 Comments

•Give The opportunity to your child with GreatToCode Kid's • Online Coding Classes for Your Kid • Introduce Your kid To the world's of coding
•Fuel You Career with our 100+ Hiring Partners, Advance Your Career in Tech with GreatToCode. •Join The Largest Tech and coding Community and Fast Forward Your career with GreatToCode. •10000+ Learner's+ 90 % placement Guarantee. • Learning Coding is Better with the GreatToCode community .
•Greattocode Kid's •GreatToCode Career •GreatToCode Interview •GreatToCode Professional •GreatToCode for schools •GreatToCode For colleges •GreatToCods For Businesses.
Are you ready to join the millions of people learning to code? GreatToCode Pass is your one-stop-shop to get access to 1000+ courses, top-notch support, and successful job placement. What are you waiting for? Sign up now and get your future in motion with GreatToCode Pass.