# How do I seperate integers?

• 12-21-2003
Guti14
How do I seperate integers?
Hi,

I'm doing this little program at the moment where I have to imput a 5-digit integer, seperate the number into its individual digits and prints the digits seperated from one another by three spaces each.
It gives me a hint that I should use the division and modulus operators.

Iv'e started like this, but really am clueless how to tackle this, can anyone give me a few tips and/or pointers

thanks for any help

Code:

int main(int argc, char* argv[])
{
int number;

cout << "Enter a five digit integer: ";
cin >> number;

number = number / 10;
cout << number;

number = number / 100;
cout << number;

number = number / 1000;
cout << number;

number = number / 10000;
cout << number;

getch();
return 0;
}

• 12-21-2003
silk.odyssey
Here's how I would do it :)

If 51236 is entered,

Code:

51236 / 10000 = 5 rem 1236
1236 /  1000 = 1 rem 236
236 /  100 = 2 rem 36
36 /    10 = 3 rem 6
6 /    1 = 6

• 12-21-2003
Guti14
yes, but the input is not pre-determined. and i have never comes across "rem" before yet.
• 12-21-2003
Codeplug
• 12-21-2003
silk.odyssey
Guti14,

rem just meant remainder. :) I know the input is not predetermined, I was just suggesting a way you could write the program.

51236 / 10000 = 5 rem 1236

51236 divided by 10000 is 5 with 1236 remaining. So we know that the first number is 5.

1236 / 1000 = 1 rem 236

1236 divided by 1000 gives us 1 with 236 remaining. So we know 1 is the second number.

236 / 100 = 2 rem 36
36 / 10 = 3 rem 6
6 / 1 = 6

Same idea for the other numbers. It's a simple algorithm and it should work for any 5 digit number.
• 12-21-2003
Zach L.
For an arbitrary number of digits, you need only compute the number of decimal digits to use in a loop. The number of digits will be floor(log10(n)) + 1.
• 12-21-2003
grib
You don't need to know the number of digits to print it out backwards, this is probably giving too much help but.
Code:

std::cout << n % 10;
while(n /= 10) std::cout << "  " << (n % 10);
std::cout << std::endl;

• 12-22-2003
Zach L.
^ True, but to print it out forward you probably should, but it could be done otherwise:

Code:

unsigned reverse(unsigned x)
{
unsigned t = 0;

do
{
t *= 10;
t += x % 10;
x /= 10;
} while(x);

return t;
}

And then grib's code would work.
• 12-22-2003
SirCrono6
silk.odyssey,

Actually, those aren't reamainders. 51236 / 10000 = 5.1236
Note the decimal. So they aren't reamainders, but decimals. Use a calculator if you don't believe me.

-SirCrono6
• 12-22-2003
grib
Sir Crono, we aren't using a calculator, we're using C++, the result of int/int is another int (truncated (a/b)*b+a%b==a).

Zach, I think your right and the op wanted it forwards, hmmm. Must come up with goofier way do to this.

Code:

void f(unsigned n) {if(n) {f(n/10); std::cout << (n % 10) << "  ";}}
• 12-22-2003
SirCrono6
Yeah, I know. I guess I was just thinking math - math. :D Int, integer, whole number. Float, float, decimal. Char, character, letter...

-SirCrono6
• 12-22-2003
Zach L.
Quote:

Originally posted by grib

Code:

void f(unsigned n) {if(n) {f(n/10); std::cout << (n % 10) << "  ";}}

Nice. :cool: