Hello guys,
I have an problem with outputting all digits of an number. I know with 2, 3, 4, 5, 6 and so but i need to write all this code for lets say 3 digits.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int main()
{
int a, b, c, d;
a = 123;
b = a % 10;
a /= 10;
c = a % 10;
a /= 10;
d = a % 10;
cout << d << " " << c << " " << b << " ";
return 0;
}
But can i somehow do this with a loop? And my question is what if n number was idk 123975234, how i can output all his digits with an loop, but not using 10000000 lines. Btw poor eng. Thanks :D :D :D
for(i = 0; i < maxdigs; i++) //you can actually get max from a log call... log10(a)+1
{
cout << a% p10 << " ";
a /= 10;
}
cout << endl; //optional cleanup
that should do better. The earlier version would write 3, 23, 123 for 123...
this one should correctly do 3, 2, 1
it does not get much more simple, but ask about what you do not understand.