nested loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nemir
    New Member
    • Sep 2007
    • 5

    nested loop

    I am new in Java and am really struggling with a this problem:

    Use nested loops that print the following patterns as shown:

    Pattern 1 Pattern 2 Pattern 3 Pattern 4
    1____________1 2 3 4 5 6 ___________ 1______1 2 3 4 5 6
    1 2 __________1 2 3 4 5 ____________2 1_______ 1 2 3 4 5
    1 2 3 ________1 2 3 4 ____________3 2 1__________1 2 3 4
    1 2 3 4_______1 2 3 ____________4 3 2 1___________1 2 3
    1 2 3 4 5_____ 1 2 ____________5 4 3 2 1____________1 2
    1 2 3 4 5 6____1_________ ___ 6 5 4 3 2 1______________ 1

    if somebody can help me..I would appreciate very much
  • nickyeng
    Contributor
    • Nov 2006
    • 252

    #2
    Code:
    for(int i = 1; i < 6; i++)
    {
            for(int k=1; k < i; k++)
            {
                    System.out.print(k);
            }
            System.out.println();
    }
    i didn't run it so you have to check for it.
    It is up to you to manipulate with it to produce 4 patterns.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by nickyeng
      Code:
      for(int i = 1; i < 6; i++)
      {
              for(int k=1; k < i; k++)
              {
                      System.out.print(k);
              }
              System.out.println();
      }
      i didn't run it so you have to check for it.
      It is up to you to manipulate with it to produce 4 patterns.
      Please don't spoonfeed code (check the forum guidelines).

      kind regards,

      Jos

      Comment

      • praveen2gupta
        New Member
        • May 2007
        • 200

        #4
        Originally posted by nemir
        I am new in Java and am really struggling with a this problem:

        Use nested loops that print the following patterns as shown:

        Pattern 1 Pattern 2 Pattern 3 Pattern 4
        1____________1 2 3 4 5 6 ___________ 1______1 2 3 4 5 6
        1 2 __________1 2 3 4 5 ____________2 1_______ 1 2 3 4 5
        1 2 3 ________1 2 3 4 ____________3 2 1__________1 2 3 4
        1 2 3 4_______1 2 3 ____________4 3 2 1___________1 2 3
        1 2 3 4 5_____ 1 2 ____________5 4 3 2 1____________1 2
        1 2 3 4 5 6____1_________ ___ 6 5 4 3 2 1______________ 1

        if somebody can help me..I would appreciate very much
        Hi
        There is no problem. You have not tryed so you don't have problem. You can't learn java in this way.

        Comment

        • nemir
          New Member
          • Sep 2007
          • 5

          #5
          Originally posted by nickyeng
          Code:
          for(int i = 1; i < 6; i++)
          {
                  for(int k=1; k < i; k++)
                  {
                          System.out.print(k);
                  }
                  System.out.println();
          }
          i didn't run it so you have to check for it.
          It is up to you to manipulate with it to produce 4 patterns.

          THIS WORKS>>THANK YOU :)

          Comment

          • nemir
            New Member
            • Sep 2007
            • 5

            #6
            Originally posted by praveen2gupta
            Hi
            There is no problem. You have not tryed so you don't have problem. You can't learn java in this way.
            believe me..I spend 3 hours on this for nothing

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by nemir
              THIS WORKS>>THANK YOU :)
              I don't think it works; have a close look at the output produced by those loops.
              It doesn't meet the requirements.

              kind regards,

              Jos

              Comment

              • nemir
                New Member
                • Sep 2007
                • 5

                #8
                Originally posted by JosAH
                I don't think it works; have a close look at the output produced by those loops.
                It doesn't meet the requirements.

                kind regards,

                Jos
                it works enough for me..I can figure out the rest of it..:)
                Thats at least i can do

                Comment

                Working...