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

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

Powered by Blogger.