Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Python Program to Find Sum Of Digits Of a Number

Find Sum Of Digits Of a Number

This Python program calculates the sum of number inserted by the user. The program is taken as an input and stored in the variable number, denoted as num.  Change the number format in array format. Initially, the sum of the variable is zero, now add one by one every element of the integer array with sum value such sum + num[i].
In this Python program to  allow the user enter any number. Finally for loop completed, then print Sum Value.
For Example:-
Enter the Number: 1568
Sum of Digits of a Number: 20
Python Program to Find Sum Of Digits Of a Number

Working:

Step 1: Start 

Step 2: Ask the user to insert an integer as an input for variable num.

Step 3. Change the number format in array format.

Step 4. Initially initialize the sum variable to 0.

Step 5. Add all array elements one by one to sum variable.

Step 6. After completing for loop, print sum value.

Step 7: Stop 

Python Program:

num = [int(d) for d in input("Enter the Number:")]
sum = 0
for i in range(0, len(num)):
    sum = sum + num[i]

print("Sum of Digits of a Number: {}".format(sum))

# This code is contributed by Shubhanshu Arya (Prepinsta Placement Cell Student) 

Output:

Enter the Number:1568
Sum of Digits of a Number: 20

4 comments on “Python Program to Find Sum Of Digits Of a Number”


  • IPE2K17

    #sum of digits of a number
    n = int(input())
    string = str(n)
    l = []
    for i in string:
    l.append(int(i))
    print(sum(l))


  • Sohana

    num = int(input(“Enter a number: “))

    sum = 0

    while(num > 0):
    rem = num % 10

    sum += rem

    num = num // 10

    print(sum)


  • Preeti

    num=int(input(“Enter the number:”))
    sum=0
    while(num!=0):
    r=num%10
    sum=sum+r
    num//=10
    print(sum)