Velocity Reviews > C++ > Using a loop to do this...

# Using a loop to do this...

snow.carriers@gmail.com
Guest
Posts: n/a

 10-01-2005
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

John Fullman
Guest
Posts: n/a

 10-01-2005

(E-Mail Removed) 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...

snow.carriers@gmail.com
Guest
Posts: n/a

 10-01-2005
Thanks, but how would you incorporate that into a while loop?

Gaijinco
Guest
Posts: n/a

 10-01-2005
> 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;

Greg
Guest
Posts: n/a

 10-01-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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