Split an Integer into its digits c++
<p>I'm trying to learn c++ on my own and I've hit a bit of a road block.  The problem is I need to take an integer,split it into its digits and get the sum of the digits and display them.</p>

<p>Example:</p>

<p>input number: <code>123456</code><br/>
digits in the integer: <code>1 2 3 4 5 6</code><br/>
sum: <code>21</code></p>

<p>I have it all done, but when I rip the integer, into digits I can't display it correctly. It displays in reverse order.</p>

<p>So in the program below I enter <code>1234</code> and it spits out <code>4 3 2 1</code>.  I know why, I just don't know how to fix it. </p>

<p>Here is my code so far:</p>

<pre><code>#include "stdafx.h"
#include &lt;cstdlib&gt;
#include &lt;iostream&gt;
#include &lt;math.h&gt;

int countDigitsInInteger(int n)
{
    int count =0;
    while(n&gt;0)
    {
       count++;
       n=n/10;
    }
    return count;
}

using namespace std;

int main(int argc, char *argv[])
{  
    int intLength =0;
    int number;
    int digit;      
    int sum = 0;
    string s;    
    cout &lt;&lt; "Please enter an integer ";
    cin &gt;&gt;number;
    cout &lt;&lt; "Orginal Number = "&lt;&lt;number &lt;&lt;endl;
    //make the number positive
    if (number&lt;0)
    number = -number;    
    intLength = countDigitsInInteger(number);
    //break apart the integer into digits
    while(number&gt;0)
    {                         
        digit = number % 10;
        number = number / 10;        
        cout &lt;&lt;digit &lt;&lt; " "; 
        sum = sum+digit; 
    } 
    cout &lt;&lt;endl &lt;&lt;"Sum of the digits is: "&lt;&lt;sum&lt;&lt;endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
</code></pre>

<p>Here is my solution</p>

<p>can't see :)</p>
