Need help in Nested Loops...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Transformers
    New Member
    • Feb 2008
    • 2

    Need help in Nested Loops...

    Please help if you can.

    Print the following patterns using nested loops. Output should be in separate programs.

    Pattern 1

    1
    12
    123
    1234
    12345
    123456

    Pattern 2

    123456
    12345
    1234
    123
    12
    1

    Pattern 3

    1
    21
    321
    4321
    54321
    654321

    Pattern 4

    123456
    12345
    1234
    123
    12
    1
  • hirak1984
    Contributor
    • Jan 2007
    • 316

    #2
    Originally posted by Transformers
    Please help if you can.

    Print the following patterns using nested loops. Output should be in separate programs.

    Pattern 1

    1
    12
    123
    1234
    12345
    123456

    Pattern 2

    123456
    12345
    1234
    123
    12
    1

    Pattern 3

    1
    21
    321
    4321
    54321
    654321

    Pattern 4

    123456
    12345
    1234
    123
    12
    1

    so did you try yourself?
    please post the code that you have written.
    we will correct it, and add something if needed.

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      There is also an interesting article in the Howtos section under Java having to do with this type of problem.

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

        Please read the Posting Guidelines and particularly the Coursework Posting Guidelines.

        Then when you are ready post a new question in this thread.

        MODERATOR

        Comment

        • Transformers
          New Member
          • Feb 2008
          • 2

          #5
          Originally posted by hirak1984
          so did you try yourself?
          please post the code that you have written.
          we will correct it, and add something if needed.

          I'm very new in Programming.
          This is what I've done but the output is only 111111....1.
          I try to insert char current = '1', '2', '3', '4', '5', '6' but it wouldn't work also.
          I know my understanding in Programming is really terrible. Please help.
          Thanks.



          public class Alphabet
          {
          public static void main(String[] args)
          {
          char current = '1';
          for(int row = 1; row <= 10; row++)
          {
          for(int column = 1; column <= 10; column++)
          {
          System.out.prin t(current + " ");
          }
          }
          }

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            Suggestion: use int not char. It may make it more obvious how to get from 1 to 2 to 3 to 4 to 5!

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              I'd suggest you write two small methods up(int n) and down(int n). The first method
              prints a line 123 ... n and the second method prints a line n ... 321 where 'n'
              is the value of the parameter you pass to the method.

              e.g. your first problem can be desribed as up(1), up(2), up(3) ... up(6) which
              screams for a simple single loop again. You can figure out the other shapes yourself.

              kind regards,

              Jos

              Comment

              Working...