#include<iostream>
#include<cstdlib>
#include<cmath>
//not needed for this program and in general bad practice
usingnamespace std;
int main()
{
int x, accum = 0; //accum is the sum of your x digits
while(x > 0)
{
accum = x % 10; //sets the last place digit of x into accum
//ex. x = 1234 now accum = 4
//this is overwritten for every iteration of your
//loop and needs to be stored
x = x / 10; //sets x to the number without last placed digit
//ex. 1234 now x = 123
}
}
Basically, you need to code it so accum not only grabs the last digit but also stores the previous digit. This is done with addition arithmetic and is identical to thought behind x += 1.
#include<iostream>
#include<cstdlib>
#include<cmath>
usingnamespace std;
int main()
{
int x, num = 0, accum = 0;
x = 666;
while(x > 0)
{
num = x % 10; //changed the orginal variable to num
accum += num; //added the addition
x = x / 10;
}
cout << accum;
}