# Thread: improve my number splitting prog

1. ## improve my number splitting prog

My following program splits a decimal number into the individual digits with the relevant multiples of 10.

My c++ skills are limited and i would like to see how other people would approach this task ,as I'm sure I've is over complicated things.

Especially this thing:-

int digit = (num % mult) / (mult / 10);

Code:
```void SplitNumber(void)
{
int num = rand() % 10000 + 1;
int nums[16] = {0};
int mult = 10;
int totalCheck = 0;
int numOfDigits = 0;

do
{
int digit = (num % mult) / (mult / 10);
nums[numOfDigits] = digit;
totalCheck += digit * (mult / 10);
mult *= 10;
numOfDigits++;
}while(totalCheck != num);

cout << "Original number = " << num << " is equal to the accumulation of\n";
//	 << "Total = " << totalCheck << '\n';

mult = 1;
for(int i = 0; i < numOfDigits; i++)
{
cout << nums[i] << " * "
<< mult  << " = "
<< nums[i] * mult << '\n';

mult *= 10;
}

}```
Mark S.

2. Code:
```#include <iostream>
#include <cmath>

int split(const int num, int iter);

int main()
{
int num;

std::cout<<"Give me a number: ";
std::cin>>num;
std::cin.ignore(1);
std::cout<<split(num,1)<<"x1"<<std::endl;

return 0;
}

int split(const int num, int iter)
{
if(num/10<1)
{
iter++;
return num;
}
else
{
std::cout<<split(num/10,++iter)<<'x'<<pow(10,iter)<<'\n';
return num-(static_cast<int>(num/10)*10);
}
}```
yeah, don't do that.

3. >i would like to see how other people would approach this task
Code:
```#include <iostream>
#include <sstream>
#include <string>

using namespace std;

string split_num ( int num )
{
stringstream s;
s<< num;
return s.str();
}

int main()
{
int num;

cout<<"Enter a number: ";

if ( cin>> num ) {
string s = split_num ( num );

for ( string::size_type i = 0; i < s.size(); i++ )
cout<< ( s[i] - '0' ) * 2 <<'\n';
}
}```