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);
Thanks for any constructive advice.
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.