nested for loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • udhay14
    New Member
    • Jun 2014
    • 1

    nested for loop

    I really need your help everyone. i need a program that results the output below using for loop

    output:
    1
    2 3
    4 5 6
    7 8 9 10
    11 12 13 14 15
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Just write your loop to display the loop counter on each cycle of the loop:

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

    Once you have that working, put an "if" statement inside the loop to display a \n after the loop counter if the counter is 1 or 3 or 6 or 10 or 15.

    Comment

    • invention
      New Member
      • Jun 2014
      • 10

      #3
      Code:
      for(i=1;i<n;i++) {
      for(j=0; j<i, j++) {
      print(i+j);
      }
      }

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You don't need nested loops. Just one loop will do:

        Code:
         
        for(i=1;i<=15;i++) {
        print(i);
        print(space);
        }
        This will print 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15.

        Now just insert an "if" statement between the two prints to print a newline (\n) if the counter i is 1 or 3 or 6 or 10 or 15.

        Comment

        Working...