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
    #1
    1. Advertising

  2. John Fullman Guest

    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
    #2
    1. Advertising

  3. Guest

    Thanks, but how would you incorporate that into a while loop?
     
    , Oct 1, 2005
    #3
  4. Gaijinco Guest

    > 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
    #4
  5. Greg Guest

    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
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VGltOjouLg==?=

    Loop the loop...

    =?Utf-8?B?VGltOjouLg==?=, Feb 16, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,385
    Karl Seguin
    Feb 16, 2005
  2. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,240
    Tim Slattery
    Mar 30, 2005
  3. -
    Replies:
    12
    Views:
    697
    Remon van Vliet
    Jun 15, 2005
  4. Byte
    Replies:
    4
    Views:
    415
  5. Isaac Won
    Replies:
    9
    Views:
    387
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page