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:

1
2
3
4
5
6
7
8
9
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 "
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
#include <iostream>
#include <math.h>

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 <<std::endl;
    
    return 0;
}



#include <iostream>

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 <<std::endl;
    
    return 0;
}
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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.