### Extracting & Reprinting Digits

Hello, this is my first post.

I am trying to write a simple program that asks the user to input an integer and then extracts and reprints all the digits separated by a space (ex. cin >> 12345, "1 2 3 4 5" is output), as well as displaying the sum of all the digits.

I am very close to the solution but for some reason my output displays the digits in reverse order (ex. cin >> 12345, "5 4 3 2 1" is output). I'm sure it's something very simple. I've tried looking at examples and searching for any hints, but I cannot get these digits flipped the right way! I would greatly appreciate any help that anyone could provide. Thank you!

Here is my code:

 ``123456789`` ``````while (num > 0){ digit = num % 10; cout << digit << " "; num = num / 10; sum = sum + digit; } cout << endl; cout << sum;``````

You could store the digits temporarily, and then use the stored data in whichever sequence you like. You could use an array, or a vector, or maybe a stringstream as the place to store the digits..
I am just confused as to why it is outputting in reverse order when the statements are in a while loop, but not otherwise. When I take the statements out of the while loop, and have it repeat a few times to test the behavior, the characters are output sequentially. Why does a loop change that??
It's not the loop causing that, it's what you're doing in the loop.
You taking the modulo 10 of the number. That gives you the right most digit.
That's what you're displaying first. You then proceed to divide by 10 and get the second digit from the right.

Do as chervil suggested and put the digits in an array.
 ``1234567891011121314151617181920`` ``````num = 12345; digit = num % 10; //digit = 12345%10 = 5 cout << digit << " "; //output "5 " num = num / 10; //num = 12345/10 = 1234 //num=1234 digit = num % 10; //digit = 1234%10 = 4 cout << digit << " "; //output "4 " num = num / 10; //num = 1234/10 = 123 //num=123 digit = num % 10; //digit = 123%10 = 3 cout << digit << " "; //output "3 " num = num / 10; //num = 123/10 = 12 //num=12 digit = num % 10; //digit = 12%10 = 2 cout << digit << " "; //output "2 " num = num / 10; //num = 12/10 = 1 //num=1 digit = num % 10; //digit = 1%10 = 1 cout << digit << " "; //output "1 " num = num / 10; //num = 1/10 = 0 ``````

without the while loop the statements still produce "5 4 3 2 1 "
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` ``````#include #include int main() { int input; std::cin >> input; int length = log10(input); while(length >= 0) { int g = input / (pow(10, length)); std::cout << g << " "; input -= g * (pow(10, length)); length--; } std::cout < int main() { std::string mystr; getline(std::cin, mystr); int sz = mystr.size(); int i = 0; while(sz >= 0) { std::cout << mystr[sz - (sz -i)] << " "; sz--; i++; } std::cout <
 Do as chervil suggested and put the digits in an array.

Or extract the number as string/c-string in the first place.

You can also do recursion:
 ``1234567891011121314151617181920`` ``````int showDigits(int i, bool firstCall = true) { if (i == 0) return 0; int prev = showDigits(i / 10, false); std::cout << i % 10; if (!firstCall) std::cout << " + "; return prev + (i % 10); } int main(void){ std::cout << "Enter an integer: "; int number; std::cin >> number; int sum = showDigits(number); std::cout << " = " << sum << '\n'; return 0; }``````
Last edited on
Topic archived. No new replies allowed.