# Python Program to Find the Sum of Digits in a Number2 min read

In this post, I’ll show you how to find digits of a given. I’ll solve the problem two diffrent way.

First way; for loop statement and other; while loop statement.

For loop:

This code prompts the user to enter a number and then calculates the sum of its digits.

Here’s a brief explanation of how the code works:

1. The “num” variable is assigned the value of the number entered by the user.
2. The “sum” variable is initialized to 0 and will be used to store the sum of the digits.
3. The for loop iterates over the digits in “num”. For each iteration, the digit is added to “sum” using the assignment operator “+=”.
4. When the loop finishes executing, the sum of the digits is printed to the console.

While loop:

This code prompts the user to enter a number and then calculates the sum of its digits.

Here’s a brief explanation of how the code works:

1. The “num” variable is assigned the value of the number entered by the user.
2. The “sumDigits” variable is initialized to 0 and will be used to store the sum of the digits.
3. The “i” variable is initialized to 0 and will be used as a counter in the while loop.
4. The while loop iterates over the digits in “num”. For each iteration, the digit is added to “sumDigits” and “i” is incremented by 1.
5. When the loop finishes executing, the sum of the digits is printed to the console.

Output: