Hi,

I am trying to write a program that will separate the digits of a non-negative integer and will print them each two spaces a apart but I have to avoid using arrays. For example if input is:

12345

then output should be:

1 2 3 4 5

I have written the following program for this purpose:

``````int main()
{
int number;
cout << "Enter a number to separate it\'s digits: ";
cin >> number;

while (number != 0)
{
cout << number%10 << "  ";
number /= 10;
}

cout << endl;

system("pause");
return 0;
}``````

It works pretty well. The only problem with this program is that it prints the digits in reverse order as they appear in the number entered i.e. for the above given example, it will print:
5 4 3 2 1
1 2 3 4 5

can someone please suggest a way to accomplish the desired behavior without using arrays or strings??

well you can start by counting the no. of digits say n
then in the loop
divisor = 10^(--n)
divide the number by divisor
and update the number as number = number % divisor

Hi,

I am trying to write a program that will separate the digits....

There is probably some nifty mathematical formula to do this with instead of the one you are using in your while loop. However, I'm not about to figure that one out!

Why not just reverse the digits first, THEN feed that number to your while loop? In fact, you could do this with a while loop similar to the one you have now. Just place this new while loop right before your existing while loop. The new while loop would work something like this:

``````while (number != 0)
{
x +=  number%10 ;
x *= 10;
number /= 10;
}``````

Then the digits in x would be 54321. Then feed that into your existing while loop as the integer to be reversed. There is just one little bug in there but it should be easy to fix and it won't cause you much trouble.

The magic word I don't see anyone using here is "logarithm."

From the Wikipedia article, a little below where I linked it:

log10(x) is related to the number of decimal digits of a positive integer x: the number of digits is the smallest integer strictly bigger than log10(x).

Once you know the number of digits, you can start at the largest one and work your way down.

@MandrewP
Your method seems to be uselessly complex and lengthy though I have understood it well too. Anyways, thanks for your help :)

@cool_zephyr and @gusano79
Thankyou very much guys for awesome hints and suggestions. I used gusano79's suggested way to detect the number of digits in the entered number and combined it with cool_zephyr's suggested way to separate them and it worked perfectly. Here is what I have come up with!! Any further suggestions of improvement??? Is it worth applying an if else structure around this program to check if the entered number is 0 and in that face output one digit 0 only? Because this program doesn't output anything when 0 is entered and simply comes to an end.

``````#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int number;
cout << "Enter a number to separate it\'s digits: ";
cin >> number;

int exponent = floor( log10( static_cast<double>(number) ) );

int divisor;
while (number != 0)
{
divisor = pow(10.0, exponent);
cout << number/divisor << "  ";
number %= divisor;
exponent--;
}

cout << endl;

system("pause");
return 0;
}``````

@MandrewP Your method seems to be uselessly complex and lengthy though I have understood it well too. Anyways, thanks for your help :)

Well actually, my method in it's compiled state runs about 30% faster than your method, and contains about 90 bytes of code compared to your 140 bytes. Also, my method is much easier to understand and thus easier and cheaper to maintain and debug. These are important considerations, especially in embedded applications.

Your method is certainly elegant, and I like to code like that. However, I find myself always in a battle to code for the smallest and fastest and most easily maintainable code verses beautiful and elegant source code. The two are not always the same.

Well actually, my method in it's compiled state runs about 30% faster than your method, and contains about 90 bytes of code compared to your 140 bytes. Also, my method is much easier to understand and thus easier and cheaper to maintain and debug. These are important considerations, especially in embedded applications.

Your method is certainly elegant, and I like to code like that. However, I find myself always in a battle to code for the smallest and fastest and most easily maintainable code verses beautiful and elegant source code. The two are not always the same.

hummm. Right! Thanks for sharing your deep insight. Now I have encountered another problem. Both the program that I posted after the help from cool_zephyr and gusano79, and the one that I developed using your suggestions, ignore any zero's which occur at the right most positions in the number. For Example, if we enter 12300 then these programs output 1 2 3 only and similarly if we enter 1000 then they only output 1. Any suggestion to overcome this??? The program that I originally posted in the thread at least outputs all the digits in the number though in reverse order. :)

For Example, if we enter 12300 then these programs output 1 2 3 only and similarly if we enter 1000 then they only output 1.

If you have 1000 and cut off the most significant digit then that becomes 000, which is equivalent to 0. This algorithm does not work when moving from MSD to LSD for that very reason.

To fix it, you can keep an index of the digit and extract it without removing it, or simply work from LSD to MSD and reverse the digits another way (such as with recursion):

``````#include <iostream>

void reverse_print_digits_r(int value, int base)
{
if (value == 0)
return;

reverse_print_digits_r(value / base, base);
std::cout << value % base << ' ';
}

void reverse_print_digits(int value, int base)
{
if (value == 0)
std::cout << "0";
else
reverse_print_digits_r(value, base);
}

int main()
{
int value;

std::cout << "Enter a value: ";

if (std::cin >> value) {
reverse_print_digits(value, 10);
std::cout << '\n';
}
}``````

@Narue

hmmm ... Thanks! I will try this soon and then will post If I encounter any problem in understanding these functions!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.