Computer Science, asked by Yakhubs4, 1 month ago

Write a Python program to find the summation of the individual digits of a number. Do not use any Library functions. Do not use any string operations. For example, if input is 25973 output should be 26.

Answers

Answered by ys909
0

Answer:

def sumOfNumbers(n):

   # store the answer in ans variable

   ans = 0

   # while number is not 0

   # take the last digit from number and add it to ans

   # divide the number by 10

   while n!=0:

       ans+=n%10

       n//=10

   return ans

print(sumOfNumbers(25973))

Explanation:

Take an example use case for dry run:

n = 25973

ans = 0  initially

iteration 1:

n%10 = 3

ans = 3

n = n//10 = 2597

iteration 2:

n%10 = 7

ans = 10

n = n//10 = 259

iteration 3:

n%10 = 9

ans = 19

n = n//10 = 25

iteration 4:

n%10 = 5

ans = 24

n = n//10 = 2

iteration 5:

n%10 = 2

ans = 26

n = n//10 = 0

Similar questions