Number of times a number can be replaced by the sum of its digits until it only contains one digit
Last Updated :
07 Aug, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int NumberofTimes(string str)
{
int temporary_sum = 0, count = 0;
while (str.length() > 1)
{
temporary_sum = 0;
for ( int i = 0; i < str.length(); i++)
temporary_sum += ( str[ i ] - '0' ) ;
str = to_string(temporary_sum) ;
count++;
}
return count;
}
int main()
{
string s = "991" ;
cout << NumberofTimes(s);
return 0;
}
|
Java
public class GFG
{
static int NumberofTimes(String str)
{
int temporary_sum = 0 , count = 0 ;
while (str.length() > 1 )
{
temporary_sum = 0 ;
for ( int i = 0 ; i < str.length(); i++)
temporary_sum += ( str.charAt(i) - '0' ) ;
str = temporary_sum + "" ;
count++;
}
return count;
}
public static void main(String[] args)
{
String s = "991" ;
System.out.println(NumberofTimes(s));
}
}
|
Python3
def NumberofTimes(s):
temporary_sum = 0
count = 0
while ( len (s) > 1 ):
temporary_sum = 0
for i in range ( len (s)):
temporary_sum + = ( ord (s[ i ]) -
ord ( '0' ))
s = str (temporary_sum)
count + = 1
return count
if __name__ = = "__main__" :
s = "991"
print (NumberofTimes(s))
|
C#
using System;
class GFG {
static int NumberofTimes(String str)
{
int temporary_sum = 0, count = 0;
while (str.Length > 1)
{
temporary_sum = 0;
for ( int i = 0; i < str.Length; i++)
temporary_sum += (str[i] - '0' );
str = temporary_sum + "" ;
count++;
}
return count;
}
public static void Main()
{
String s = "991" ;
Console.Write(NumberofTimes(s));
}
}
|
PHP
<?php
function NumberofTimes( $str )
{
$temporary_sum = 0; $count = 0;
while ( strlen ( $str ) > 1)
{
$temporary_sum = 0;
for ( $i = 0; $i < strlen ( $str ); $i ++)
$temporary_sum += ( $str [ $i ] - '0' );
$str = (string)( $temporary_sum );
$count ++;
}
return $count ;
}
$s = "991" ;
echo NumberofTimes( $s );
?>
|
Javascript
<script>
function NumberofTimes(str)
{
var temporary_sum = 0, count = 0;
while (str.length > 1)
{
temporary_sum = 0;
for (i = 0; i < str.length; i++)
temporary_sum += ( str.charAt(i) - '0' ) ;
str = temporary_sum + "" ;
count++;
}
return count;
}
var s = "991" ;
document.write(NumberofTimes(s));
</script>
|
Output:
3
Time complexity: O(d2) where d is no of digits in given number n
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...