Split an Integer into Its Digits C++

C: how to break apart a multi digit number into separate variables?

int value = 123;
while (value > 0) {
int digit = value % 10;
// do something with digit
value /= 10;
}

How do I split an int into its digits?

Given the number 12345 :

5 is 12345 % 10
4 is 12345 / 10 % 10
3 is 12345 / 100 % 10
2 is 12345 / 1000 % 10
1 is 12345 / 10000 % 10

I won't provide a complete code as this surely looks like homework, but I'm sure you get the pattern.

Split an Integer into its digits c++

Your problem comes from the fact that you are reading the digits backwards, thus you need to print them out backwards. A stack will help you tremendously.

#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <stack>

int countDigitsInInteger(int n)
{
int count =0;
while(n>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 << "Please enter an integer ";
cin >>number;
cout << "Orginal Number = "<<number <<endl;
//make the number positive
if (number<0)
number = -number;

intLength = countDigitsInInteger(number);
//break apart the integer into digits

stack<int> digitstack;
while(number>0)
{
digit = number % 10;
number = number / 10;
digitstack.push(digit);
sum = sum+digit;
}

while(digitstack.size() > 0)
{
cout << digitstack.top() << " ";
digitstack.pop();
}

cout <<endl <<"Sum of the digits is: "<<sum<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}

Oh, and BTW, keep your indentation clean. Its important.

EDIT: In response to Steve Townsend, this method is not necessarily overkill, it is just different from yours. The code can be slimmed down so that it seems less like overkill:

#include <iostream>
#include <stack>
#include <string>

using namespace std;

int getInput(string prompt)
{
int val;
cout << prompt;
cin >> val;
return val < 0 ? -val : val;
}

int main(int argc, char** argv)
{
int num = getInput("Enter a number: ");
cout << "Original Number: " << num << endl;

stack<int> digits;
int sum = 0;
while(num > 0)
{
digits.push(num % 10);
sum += digits.top();
num = num / 10;
}

while(digits.size() > 0)
{
cout << digits.top() << " ";
digits.pop();
}

cout << endl << "Sum of digits is " << sum << endl;

return 0;
}

Split Integer into two separate Integers

Here is a demonstrative program. It does not use any function except printf.:) Thus it is the simplest solution.

#include <stdio.h>

int main( void )
{
unsigned int a[] = { 12, 1234, 123456, 12345678, 1234567890 };
const unsigned int Base = 10;

for ( size_t i = 0; i < sizeof( a ) / sizeof( *a ); i++ )
{
unsigned int divisor = Base;
while ( a[i] / divisor > divisor ) divisor *= Base;

printf( "%u\t%u\n", a[i] / divisor, a[i] % divisor );
}
}

The program output is

1       2
12 34
123 456
1234 5678
12345 67890

If you are going to use a signed integer type and negative numbers then the program can look the following way

#include <stdio.h>

int main( void )
{
int a[] = { -12, 1234, -123456, 12345678, -1234567890 };
const int Base = 10;

for ( size_t i = 0; i < sizeof( a ) / sizeof( *a ); i++ )
{
int divisor = Base;
while ( a[i] / ( a[i] < 0 ? -divisor : divisor ) > divisor ) divisor *= Base;

printf( "%d\t%d\n", a[i] / divisor, a[i] % divisor );
}
}

Its output is

-1      -2
12 34
-123 -456
1234 5678
-12345 -67890

Is there a more efficient way of splitting a number into its digits?

You could do subtractions in a loop with predefined base 10 values.

My C is a bit rusty, but something like this:

int num[] = { 10000000,1000000,100000,10000,1000,100,10,1 };

for (pos = 0; pos < 8; pos++) {
int cnt = 0;
while (val >= num[pos]) {
cnt++;
val -= num[pos];
}
LCD_Display(pos, cnt);
}


Related Topics



Leave a reply



Submit