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

RESTful APIs with CRUD Operations in Laravel 12| (2025)

  RESTful APIs serve as the foundation of modern web development. They follow a set of rules called Representational State Transfer (REST) t...

Powered by Blogger.