i'm trying to write a function which takes one int parameter and returns sum of it's digits.For example, digital_root(123) will return 1+2+3 which is 6.And inside the for loop i can't convert individual character to integer..
it should be included that i used both atoi() and stoi() functions.What is wrong with the code?
int digital_root(int x)
{
int t = 0;
string str = to_string(x);
for(char& c : str){
t += atoi(c);
}
return t;
}
i expect the characters to convert to integer successfully.how can i do so?