-
Notifications
You must be signed in to change notification settings - Fork 0
/
digital_root.cpp
111 lines (88 loc) · 1.76 KB
/
digital_root.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
digital_root.cpp
Aulas particulares de programação C++
CodeWars exercise:
Digital root is the recursive sum of all the digits in a number.
Given n, take the sum of the digits of n.
If that value has more than one digit, continue reducing in this way
until a single-digit number is produced.
The input will be a non-negative integer.
Examples
16 --> 1 + 6 = 7
942 --> 9 + 4 + 2 = 15 --> 1 + 5 = 6
132189 --> 1 + 3 + 2 + 1 + 8 + 9 = 24 --> 2 + 4 = 6
493193 --> 4 + 9 + 3 + 1 + 9 + 3 = 29 --> 2 + 9 = 11 --> 1 + 1 = 2
*/
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <sstream>
using std::stringstream;
#include <string>
using std::stoi;
using std::string;
#include <stdlib.h>
int digital_root(int n);
int main(){
int n,r;
cout << "Entre com um inteiro positivo: ";
cin >> n;
if(n < 0) exit(0);
r = digital_root(n);
printf("%d\n",r);
return 0;
}
int digital_root(int n){
int nr,aux,i,x;
stringstream buff;
aux = n;
while(aux>=10){
nr=0;
buff << aux;
string nrstr;
buff >> nrstr;
//cout << "nrstr: " << nrstr << endl;
for(i=0;i<nrstr.length();i++){
//cout << nrstr[i] << endl;
switch(nrstr[i]){
case '0':
x=0;
break;
case '1':
x=1;
break;
case '2':
x=2;
break;
case '3':
x=3;
break;
case '4':
x=4;
break;
case '5':
x=5;
break;
case '6':
x=6;
break;
case '7':
x=7;
break;
case '8':
x=8;
break;
case '9':
x=9;
}
nr += x;
}
//cout << "soma: " << nr << endl;
aux = nr;
buff.clear(); // clear buff variable
}
return aux;
}
// end of digital_root.cpp