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 925 (Div. 3) 1931D. Divisible Pairs Solution

    Problem Link  :   https://codeforces.com/contest/1931/problem/D S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. ...

Powered by Blogger.