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 firstname.lastname@example.org. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Finding sum of digits of a number until sum becomes single digit
- Maximum of sum and product of digits until number is reduced to a single digit
- Convert a number of length N such that it contains any one digit at least 'K' times
- Sum of the digits of square of the given number which has only 1's as its digits
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Sum of all numbers formed having 4 atmost X times, 5 atmost Y times and 6 atmost Z times
- Smallest N digit number with none of its digits as its divisor
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Find the number of integers from 1 to n which contains digits 0's and 1's only
- Number formed by adding product of its max and min digit K times
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- How to check if string contains only digits in Java
- Check if given number contains a digit which is the average of all other digits
- Find N numbers such that a number and its reverse are divisible by sum of its digits
- Count of numbers from the range [L, R] which contains at least one digit that divides K
- Count numbers up to N which contains at least one repeated digit
- Find the remainder when First digit of a number is divided by its Last digit
- Check if the sum of digits of number is divisible by all of its digits
- Count of N-digit numbers having digit XOR as single digit