Using a loop to do this...

S

snow.carriers

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
 
J

John Fullman

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...
 
G

Gaijinco

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;
 
G

Greg

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,042
Latest member
icassiem

Latest Threads

Top