# Thread: Writing program that prints out each digit of an entered number, loop question

1. Originally Posted by Kelton2 I got it to work with recursion, but it prints one too many comma and IDK how to fix it:
Can someone help?
This is a cosmetic issue. It's an issue an if can solve though. Then the number is first divided, you don't need to print a comma, but at all other times, you do. 2. Originally Posted by whiteflags Then the number is first divided
How would I check for that? 3. One way is to turn the recursive function into a helper function which is called from your actual function. This actual function will not print the comma, but it does call the recursive function that does print the comma. 4. Originally Posted by laserlight One way is to turn the recursive function into a helper function which is called from your actual function. This actual function will not print the comma, but it does call the recursive function that does print the comma. 5. Originally Posted by Kelton2
No, at the moment you don't have a recursive helper function. Your explode function that only has one parameter is both the function called in main and the recursive function, whereas my suggestion is to make the function called in main non-recursive and not print the comma, and have that non-recursive function call the recursive helper function that prints the comma. 6. I have changed it back to iteration and I now have:
Code:
```#include <iostream>
using namespace std;
int explode(int number,int array[])
{   int variable = number;
int numberOfDigits = 0;
int digit = number % 10;
array[digit] = digit;
while (number > 0) {
int digit = number % 10;
int numbe = number;
array[numberOfDigits] = digit;
cout << array[numberOfDigits];
number /= 10;
numberOfDigits++;
}
int i = 0;
int save = 0;
save = numberOfDigits;
for(i = 0; i < numberOfDigits/2; i++) {
array[i] = array[save--];
//    cout << "Starting arrayprint:";
//  cout << array[i] << endl;
}
//    cout << "End of arrayprint";
//  return array[i];
/* while (number > 0) {
number /= 10;
numberOfDigits++;
} */

return numberOfDigits;
}
/*int printDigits(int number,int array[])
{
int digit = number % 10;
array[digit] = digit;
while (number > 0) {
int digit = number % 10;
int numbe = number;
array[digit] = digit;
cout << array[digit];
number /= 10;
}
int i = 0;
for(i = 0; i < number/2; i++) {
array[i] = number-i-1;
}
return array[i];

} */

int main ( ) {
int number;
int array;
cout << "Enter number: ";
cin >> number;
cout << "[";
explode(number,array);
cout << ",";
cout << "]";
}```
It prints backwards. How do I fix it? 7. Update: It still doesn't work, but I have updated my code. It now has a revised main and prints in a different way. My code is below:
Code:

Code:
```#include <iostream>#include <iomanip>
#include <cmath>
using namespace std;
int explode(int number,int array[])
{   int variable = number;
int numberOfDigits = 0;
int digit = number % 10;
array[digit] = digit;

while (number > 0) {
int digit = number % 10;
int numbe = number;
array[numberOfDigits] = digit;
//  cout << array[numberOfDigits];
number /= 10;
numberOfDigits++;
}
int i = 0;
int save = 0;
save = numberOfDigits;
for(i = 0; i < numberOfDigits/2; i++) {
array[i] = array[i];
//  cout << "Starting arrayprint:";
//    cout << array[i] << endl;
}
//    cout << "End of arrayprint";
//  return array[i];
/* while (number > 0) {
number /= 10;
numberOfDigits++;
} */

return numberOfDigits;
}
/*int printDigits(int number,int array[])
{
int digit = number % 10;
array[digit] = digit;
while (number > 0) {
int digit = number % 10;
int numbe = number;
array[digit] = digit;
cout << array[digit];
number /= 10;
}
int i = 0;
for(i = 0; i < number/2; i++) {
array[i] = number-i-1;
}
return array[i];

} */

int main()
{
int digits;
int numdigits;

int n;
cout << "Enter number: ";
cin >> n;

numdigits = explode(n,digits);
cout << "The following is numdigits: " << numdigits;
cout << endl;
cout << "[" << digits;
for( int i = 1; i < numdigits; i++ )
cout << "," << digits[i];
cout << "]" << endl;
}```
Can someone help? 8. Nevermind, solved. 9. If you can figure out the number of digits the number has in advance, you can use a plain loop to store the numbers in correct order:

Code:
```void extractDigits (int number, int* array, int sizeArray)
{
for (int i = std::min(sizeArray - 1, numberDigits(number)); i >= 0; i--) {
array[i] = number % 10;
number /= 10;
}
}```
As for how to get the number of digits a number will have, a big hint is to consider the underlined expression in the for loop above (number /= 10). This expression evaluates to zero when there are no more digits left in a number. You can use this to get the number of digits. Popular pages Recent additions 