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.**Examples:**

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 .

## C++

`// C++ program to count number of times we` `// need to add digits to get a single digit.` `#include <bits/stdc++.h>` `using` `namespace` `std;` `int` `NumberofTimes(string str)` `{` ` ` `// Here the count variable store` ` ` `// how many times we do sum of` ` ` `// digits and temporary_sum` ` ` `// always store the temporary sum` ` ` `// we get at each iteration .` ` ` `int` `temporary_sum = 0, count = 0;` ` ` `// In this loop we always compute` ` ` `// the sum of digits in temporary_` ` ` `// sum variable and convert it` ` ` `// into string str till its length` ` ` `// become 1 and increase the count` ` ` `// in each iteration.` ` ` `while` `(str.length() > 1)` ` ` `{` ` ` `temporary_sum = 0;` ` ` `// computing sum of its digits` ` ` `for` `(` `int` `i = 0; i < str.length(); i++)` ` ` `temporary_sum += ( str[ i ] - ` `'0'` `) ;` ` ` `// converting temporary_sum into string` ` ` `// str again .` ` ` `str = to_string(temporary_sum) ;` ` ` `// increase the count` ` ` `count++;` ` ` `}` ` ` `return` `count;` `}` `// Driver program to test the above function` `int` `main()` `{` ` ` `string s = ` `"991"` `;` ` ` `cout << NumberofTimes(s);` ` ` `return` `0;` `}` |

## Java

`// Java program to count number of times we` `// need to add digits to get a single digit.` `public` `class` `GFG` `{` ` ` `static` `int` `NumberofTimes(String str)` ` ` `{` ` ` `// Here the count variable store` ` ` `// how many times we do sum of` ` ` `// digits and temporary_sum` ` ` `// always store the temporary sum` ` ` `// we get at each iteration .` ` ` `int` `temporary_sum = ` `0` `, count = ` `0` `;` ` ` ` ` `// In this loop we always compute` ` ` `// the sum of digits in temporary_` ` ` `// sum variable and convert it` ` ` `// into string str till its length` ` ` `// become 1 and increase the count` ` ` `// in each iteration.` ` ` `while` `(str.length() > ` `1` `)` ` ` `{` ` ` `temporary_sum = ` `0` `;` ` ` ` ` `// computing sum of its digits` ` ` `for` `(` `int` `i = ` `0` `; i < str.length(); i++)` ` ` `temporary_sum += ( str.charAt(i) - ` `'0'` `) ;` ` ` ` ` `// converting temporary_sum into string` ` ` `// str again .` ` ` `str = temporary_sum + ` `""` `;` ` ` ` ` `// increase the count` ` ` `count++;` ` ` `}` ` ` ` ` `return` `count;` ` ` `}` ` ` ` ` `// Driver program to test above functions` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `String s = ` `"991"` `;` ` ` `System.out.println(NumberofTimes(s));` ` ` `}` `}` `/* This code is contributed by Mr. Somesh Awasthi */` |

## Python3

`# Python 3 program to count number of times we` `# need to add digits to get a single digit.` `def` `NumberofTimes(s):` ` ` `# Here the count variable store` ` ` `# how many times we do sum of` ` ` `# digits and temporary_sum` ` ` `# always store the temporary sum` ` ` `# we get at each iteration .` ` ` `temporary_sum ` `=` `0` ` ` `count ` `=` `0` ` ` `# In this loop we always compute` ` ` `# the sum of digits in temporary_` ` ` `# sum variable and convert it` ` ` `# into string str till its length` ` ` `# become 1 and increase the count` ` ` `# in each iteration.` ` ` `while` `(` `len` `(s) > ` `1` `):` ` ` ` ` `temporary_sum ` `=` `0` ` ` `# computing sum of its digits` ` ` `for` `i ` `in` `range` `(` `len` `(s)):` ` ` `temporary_sum ` `+` `=` `(` `ord` `(s[ i ]) ` `-` ` ` `ord` `(` `'0'` `))` ` ` `# converting temporary_sum into` ` ` `# string str again .` ` ` `s ` `=` `str` `(temporary_sum)` ` ` `# increase the count` ` ` `count ` `+` `=` `1` ` ` `return` `count` `# Driver Code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` ` ` `s ` `=` `"991"` ` ` `print` `(NumberofTimes(s))` `# This code is contributed by Ita_c` |

## C#

`// C# program to count number of` `// times we need to add digits to` `// get a single digit.` `using` `System;` `class` `GFG {` ` ` ` ` `// Function to count number of` ` ` `// times we need to add digits` ` ` `// to get a single digit` ` ` `static` `int` `NumberofTimes(String str)` ` ` `{` ` ` ` ` `// Here the count variable store` ` ` `// how many times we do sum of` ` ` `// digits and temporary_sum` ` ` `// always store the temporary sum` ` ` `// we get at each iteration .` ` ` `int` `temporary_sum = 0, count = 0;` ` ` ` ` `// In this loop we always compute` ` ` `// the sum of digits in temporary_` ` ` `// sum variable and convert it` ` ` `// into string str till its length` ` ` `// become 1 and increase the count` ` ` `// in each iteration.` ` ` `while` `(str.Length > 1)` ` ` `{` ` ` `temporary_sum = 0;` ` ` ` ` `// computing sum of its digits` ` ` `for` `(` `int` `i = 0; i < str.Length; i++)` ` ` `temporary_sum += (str[i] - ` `'0'` `);` ` ` ` ` `// converting temporary_sum` ` ` `// into string str again .` ` ` `str = temporary_sum + ` `""` `;` ` ` ` ` `// increase the count` ` ` `count++;` ` ` `}` ` ` ` ` `return` `count;` ` ` `}` ` ` ` ` `// Driver code` ` ` `public` `static` `void` `Main()` ` ` `{` ` ` `String s = ` `"991"` `;` ` ` `Console.Write(NumberofTimes(s));` ` ` `}` `}` `// This code is contributed by Nitin Mittal.` |

## PHP

`<?php` `// PHP program to count number of times we` `// need to add digits to get a single digit.` `function` `NumberofTimes(` `$str` `)` `{` ` ` `// Here the count variable store` ` ` `// how many times we do sum of` ` ` `// digits and temporary_sum` ` ` `// always store the temporary sum` ` ` `// we get at each iteration .` ` ` `$temporary_sum` `= 0; ` `$count` `= 0;` ` ` `// In this loop we always compute` ` ` `// the sum of digits in temporary_` ` ` `// sum variable and convert it` ` ` `// into string str till its length` ` ` `// become 1 and increase the count` ` ` `// in each iteration.` ` ` `while` `(` `strlen` `(` `$str` `) > 1)` ` ` `{` ` ` `$temporary_sum` `= 0;` ` ` `// computing sum of its digits` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `strlen` `(` `$str` `); ` `$i` `++)` ` ` `$temporary_sum` `+= (` `$str` `[ ` `$i` `] - ` `'0'` `);` ` ` `// converting temporary_sum into` ` ` `// string str again .` ` ` `$str` `= (string)(` `$temporary_sum` `);` ` ` `// increase the count` ` ` `$count` `++;` ` ` `}` ` ` `return` `$count` `;` `}` `// Driver Code` `$s` `= ` `"991"` `;` `echo` `NumberofTimes(` `$s` `);` `// This code is contributed` `// by Akanksha Rai` `?>` |

## Javascript

`<script>` `// Javascript program to` `// count number of times we` `// need to add digits to` `// get a single digit.` `function` `NumberofTimes(str)` `{` ` ` `// Here the count variable store` ` ` `// how many times we do sum of` ` ` `// digits and temporary_sum` ` ` `// always store the temporary sum` ` ` `// we get at each iteration .` ` ` `var` `temporary_sum = 0, count = 0;` ` ` ` ` `// In this loop we always compute` ` ` `// the sum of digits in temporary_` ` ` `// sum variable and convert it` ` ` `// into string str till its length` ` ` `// become 1 and increase the count` ` ` `// in each iteration.` ` ` `while` `(str.length > 1)` ` ` `{` ` ` `temporary_sum = 0;` ` ` ` ` `// computing sum of its digits` ` ` `for` `(i = 0; i < str.length; i++)` ` ` `temporary_sum += ( str.charAt(i) - ` `'0'` `) ;` ` ` ` ` `// converting temporary_sum into string` ` ` `// str again .` ` ` `str = temporary_sum + ` `""` `;` ` ` ` ` `// increase the count` ` ` `count++;` ` ` `}` ` ` ` ` `return` `count;` `}` `// Driver program to test above functions` `var` `s = ` `"991"` `;` `document.write(NumberofTimes(s));` `// This code contributed by Princi Singh` `</script>` |

**Output:**

3

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 contribute@geeksforgeeks.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 mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**