Hackerrank Recursive Digit Sum Solution
Solution in C++:
///**********ALLAH IS ALMIGHTY************///
///AH Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll sum,k,i;
string n;
ll fun(ll v)
{
if(v<10)
return v;
sum=0;
while(v)
{
sum+=v%10;
v/=10;
}
return fun(sum);
}
int main()
{
cin>>n>>k;
for(i=0; i<n.length(); i++)
sum=sum+n[i]-'0';
//cout<<sum<<endl;
cout<<fun((sum*k))<<endl;
}
No comments