Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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

 
Reply With Quote
 
 
 
 
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...

 
Reply With Quote
 
 
 
 
snow.carriers@gmail.com
Guest
Posts: n/a
 
      10-01-2005
Thanks, but how would you incorporate that into a while loop?

 
Reply With Quote
 
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;

 
Reply With Quote
 
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Getting a loop to activate a loop above it Byte Python 4 03-24-2006 03:04 AM
Condition outside loop or separate loop for different condition? - Java 12 06-15-2005 08:50 AM
while loop in a while loop Steven Java 5 03-30-2005 09:19 PM
Loop the loop... =?Utf-8?B?VGltOjouLg==?= ASP .Net 2 02-16-2005 12:21 PM



Advertisments