Sum of Digit of a Number Without Recursion in Python

This is a Python Program to find the sum of digits in a number without using recursion.

Problem Description

The program takes a number and finds the sum of digits without using recursion.

Problem Solution

1. Take a number from the user.
2. Using a while loop, obtain each digit and append it to the list.
3. Use the sum() function to obtain the sum of digits in the list.
4. Exit.

Program/Source Code

Here is source code of the Python Program to find the sum of digits in a number without using recursion. The program output is also shown below.

l=[]
b=int(input("Enter a number: "))
while(b>0):
    dig=b%10
    l.append(dig)
    b=b//10
print("Sum is:")
print(sum(l))
Program Explanation

1. User must enter a number.
3. The condition for the while loop is that the number should be greater than 0.
4. Otherwise, each digit is obtain and appended to the list.
5. The sum of elements in the list is obtained using the sum() function and printed which is the sum of digits of the number.

advertisement
Runtime Test Cases
 
Case 1:
Enter a number: 123
Sum is:
6
 
Case 2:
Enter a number: 567
Sum is:
18

Sanfoundry Global Education & Learning Series – 1000 Python Programs.

If you wish to look at all Python Programming examples, go to 1000 Python Programs.

⚡ Claim Your Free Java Certification - January 2026

👉 For weekly programming practice and certification updates, join Sanfoundry’s official WhatsApp & Telegram channels

advertisement
Manish Bhojasia – Founder & CTO at Sanfoundry

I’m Manish, Founder & CTO at Sanfoundry, with 25+ years of experience across Linux systems, SAN technologies, advanced C programming, and building large-scale, performance-driven learning and certification platforms focused on clear skill validation.

LinkedIn  ·  YouTube MasterClass  ·  Telegram Classes  ·  Career Guidance & Conversations