Suppose we have a number num, we have to find the sum of its digits. We have to solve it without using strings.
So, if the input is like num = 512, then the output will be 8, as 8 = 5 + 1 + 2.
tput will be 8, as 8 = 5 + 1 + 2. To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution: def solve(self, num): sum=0 while(num!=0): sum = sum+int(num%10) num=int(num/10) return sum ob = Solution() print(ob.solve(512))