# Using a loop to do this...

Discussion in 'C++' started by snow.carriers@gmail.com, Oct 1, 2005.

1. ### Guest

Basically getting a five digit number with a long unsigned variable,
and making the output like this:
number is 63424
6
3
4
2
4

Using loops, does anyone know how to start this? I'm thinking of using
powers of 10, % and a while loop...

Thanks

, Oct 1, 2005

2. ### John FullmanGuest

wrote:
> Basically getting a five digit number with a long unsigned variable,
> and making the output like this:
> number is 63424
> 6
> 3
> 4
> 2
> 4
>
> Using loops, does anyone know how to start this? I'm thinking of using
> powers of 10, % and a while loop...
>
> Thanks

On the right track.
(63424 - 63420) / 1 = 4
(63420 - 63400) / 10 = 2
etc...

John Fullman, Oct 1, 2005

3. ### Guest

Thanks, but how would you incorporate that into a while loop?

, Oct 1, 2005
4. ### GaijincoGuest

> Using loops, does anyone know how to start this? I'm thinking of using
> powers of 10, % and a while loop...

Does this helps?

unsigned long number;
cin >> number;

for(int times=0, dig=10000; times < 5; ++times, dig/=10){
cout << number/dig%10 << endl;

Gaijinco, Oct 1, 2005
5. ### GregGuest

wrote:
> Thanks, but how would you incorporate that into a while loop?

It seems odd that the implementation has to use a while loop. A
loopless solution is perfectly reasonable:

#include <iostream>

void PrintDigits(int num)
{
if (num > 9)
PrintDigits(num/10);

std::cout << num%10 << std::endl;
}

int main()
{
PrintDigits(12345);
}

Output:
1
2
3
4
5

Surely the above solution would work just as well for your purposes.

Greg

Greg, Oct 1, 2005