Repetitive Addition Of Digits
PROBLEM :
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
Input:
The first line contains 'T' denoting the number of testcases. Then follows description of testcases. The next T lines contains a single integer N denoting the value of N.
Output:
Output the sum of all its digit until the result has only one digit.
Constraints:
1<=T<=30
1<=n<=10^9
Example:
Input :
2
1
98
Output :
1
8
Explanation: For example, if we conisder 98, we get 9+8 = 17 after first addition. Then we get 1+7 = 8. We stop at this point because we are left with one digit.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int Repetitive_Addition(long long ) ;
int main()
{
long long no ;
int t ;
cin>>t ;
while(t--)
{
cin>>no ;
cout<<Repetitive_Addition(no)<<endl ;
}
return 0;
}
int Repetitive_Addition(long long no)
{
long long sum=0 ;
int rem ;
while(no)
{
rem=no%10 ;
sum=sum+rem ;
no=no/10 ;
}
if(sum>=10)
return Repetitive_Addition(sum) ;
return sum ;
}
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int Repetitive_Addition(long long ) ;
int main()
{
long long no ;
int t ;
cin>>t ;
while(t--)
{
cin>>no ;
cout<<Repetitive_Addition(no)<<endl ;
}
return 0;
}
int Repetitive_Addition(long long no)
{
if(no%9==0)
return 9 ;
return no%9 ;
}
---------------------------------------------------------------------------------
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
Input:
The first line contains 'T' denoting the number of testcases. Then follows description of testcases. The next T lines contains a single integer N denoting the value of N.
Output:
Output the sum of all its digit until the result has only one digit.
Constraints:
1<=T<=30
1<=n<=10^9
Example:
Input :
2
1
98
Output :
1
8
Explanation: For example, if we conisder 98, we get 9+8 = 17 after first addition. Then we get 1+7 = 8. We stop at this point because we are left with one digit.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int Repetitive_Addition(long long ) ;
int main()
{
long long no ;
int t ;
cin>>t ;
while(t--)
{
cin>>no ;
cout<<Repetitive_Addition(no)<<endl ;
}
return 0;
}
int Repetitive_Addition(long long no)
{
long long sum=0 ;
int rem ;
while(no)
{
rem=no%10 ;
sum=sum+rem ;
no=no/10 ;
}
if(sum>=10)
return Repetitive_Addition(sum) ;
return sum ;
}
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int Repetitive_Addition(long long ) ;
int main()
{
long long no ;
int t ;
cin>>t ;
while(t--)
{
cin>>no ;
cout<<Repetitive_Addition(no)<<endl ;
}
return 0;
}
int Repetitive_Addition(long long no)
{
if(no%9==0)
return 9 ;
return no%9 ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment