Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

for loop challenge

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


    shouldn't the last line be: 7890 ?


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