Python

Python Program to Find the Sum of Digits in a Number using while loop2 min read

In this example, You can find  the sum of digits of a number that given by user using while loop statement

Python Code:




Output:

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

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.

Leave a Comment