satya

Wednesday, October 7, 2020

Program 17

                                                                         Program 17

17.Write a function called sum_digits that is given an integer num and returns the sum of the digits of num.


def sum_digit(num):

    sum1=0

    while(num!=0):

        sum1=sum1+num%10

        num=num//10

    return(sum1)

num=int(input("enter the integer number:"))

sum1=sum_digit(num)

print("the sum of %d digits are =%d"%(num,sum1))

output:

=======RESTART:D:/old/r19lab-python/18.py  =====================

enter the integer number:164

the sum of 164 digits are =11

>>> 

=======RESTART:D:/old/r19lab-python/18.py =====================

enter the integer number:124

the sum of 124 digits are =7

>>> 



No comments:

Post a Comment

Program 30

                                                                         Program 30 Write a Python class to implement pow(x, n). class Pow1:...