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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

for loop challenge

  • 20-07-2009 10: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, Registered Users 2 Posts: 3,323 ✭✭✭padraig_f


    shouldn't the last line be: 7890 ?


  • Registered Users, Registered Users 2 Posts: 3,323 ✭✭✭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