Header Ads Widget

Python program to find the sum of digits of a number

 

 Python program to find the sum of digits of a number 

Given a number , write a program to find the sum of digits of number in Python . The logic behind to do this is first we will find the digits of the number by finding reminder by dividing number by 10 and each time add this reminder in a variable named as sum .

Sample input :

123

Sample output:

6

 Python program to find the sum of digits of a number 

The objective of the code is to find the sum of the digits of a number . 

Code(Python):-

num=int(input("Enter a number\n"))
sum_digits=0
while(num!=0):
r=num%10
sum_digits+=r
num//=10
print("Number of digits is ",sum_digits)

Output:

Enter a number
123
Sum of digits is 6



Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-