Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

for loop challenge

Options
  • 20-07-2009 11:41am
    #1
    Closed Accounts Posts: 1,780 ✭✭✭


    Hi,

    Does anyone know how to solve this? I can do it using an external variable outside the loops, but what's the best way?

    Use nested fors to solve this...

    1
    23
    456
    78910


Comments

  • Registered Users Posts: 3,287 ✭✭✭padraig_f


    shouldn't the last line be: 7890 ?


  • Registered Users Posts: 3,287 ✭✭✭padraig_f


    oh I see, that's a 10 at the end, thought it was 1 followed by 0.

    Here's my effort. There's nothing wrong with using an external variable. I declared it as part of the for statement.
    for( int i = 1, length = 1;  i <= 10; i += length++ )
    {
       for( int j = i; j < (i+length);  j++ )
          cout<< j % 10;
       cout<< endl;
    }
    

    I modded the output with 10 so it gives a more pleasing pattern:
    1
    23
    456
    7890
    


Advertisement