Count the number of times a number can be replaced by the sum of its digits until it only contains one digit and number can be very large.
Input : 10 Output : 1 1 + 0 = 1, so only one times an number can be replaced by its sum . Input : 991 Output : 3 9 + 9 + 1 = 19, 1 + 9 = 10, 1 + 0 = 1 hence 3 times the number can be replaced by its sum.
We have discussed Finding sum of digits of a number until sum becomes single digit.
The problem here is just extension of the above previous problem. Here, we just want to count number of times a number can be replaced by its sum until it only contains one digit. As number can be very much large so to avoid overflow, we input the number as string. So, to compute this we take one variable named as temporary_sum in which we repeatedly calculate the sum of digits of string and convert this temporary_sum into string again. This process repeats till the string length becomes 1 . To explain this in a more clear way consider number 991
9 + 9 + 1 = 19, Now 19 is a string
1 + 9 = 10, again 10 is a string
1 + 0 = 1 . again 1 is a string but here string length is 1 so, loop breaks .
The number of sum operations is the final answer .
Below is implementation of this approach .
This article is contributed by Surya Priy. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Convert a number of length N such that it contains any one digit at least 'K' times
- Finding sum of digits of a number until sum becomes single digit
- Find last five digits of a given five digit number raised to power five
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Maximum of sum and product of digits until number is reduced to a single digit
- Minimum number of 1's to be replaced in a binary array
- Number of times the largest perfect square number can be subtracted from N
- Find the Largest number with given number of digits and sum of digits
- Find smallest number with given number of digits and sum of digits
- Count of Numbers in Range where first digit is equal to last digit of the number
- Queries on sum of odd number digit sums of all the factors of a number
- Largest number less than N with digit sum greater than the digit sum of N
- Largest number less than N whose each digit is prime number
- Find count of digits in a number that divide the number