Skip to content

Commit

Permalink
Create RecursiveDigitSum.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnabBir authored Jan 23, 2018
1 parent 27dd705 commit e2dede4
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions RecursiveDigitSum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <bits/stdc++.h>

using namespace std;

int digitSum(string n, int k) {

if(n.length() == 1 && k == 1){
return (int) (n[0]-'0');
}
long sum = 0;
if( k == 1){
for(int i = 0; i < n.length(); ++i){
sum += (int) (n[i] - '0');
}
return digitSum(to_string(sum), 1);
}
else{
return digitSum(to_string(k * (int)digitSum(n , 1)),1);
}

}

int main() {
string n;
int k;
cin >> n >> k;
int result = digitSum(n, k);
cout << result << endl;
return 0;
}

0 comments on commit e2dede4

Please sign in to comment.