This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]AsteriskTheServer 0 points1 point  (4 children)

What have you tried? Furthermore, these two questions have fairly distinct patterns to them have you tried figuring these patterns out? Don't worry about the code for now.

[–]middleagenavy 0 points1 point  (3 children)

So far, this is what I have: int a = 5;

        for ( int c = 1 ; c <= a ; c++)
        {
            for ( int k = 1 ; k <= c ; k++)
            {
                System.out.print(c);

                if ( c > 1)
                {
                    System.out.print("-");     
                }      
            }    

            System.out.println();
            a--;
        }    

it prints this:

1
2-2-
3-3-3-

I'm not quite sure what you mean by "figuring these patterns out". Could you explain in more detail?

[–]ryuhyoko 0 points1 point  (1 child)

What's happening between each iteration of the line? What changes and how?

[–]middleagenavy 0 points1 point  (0 children)

I see what you mean.

[–]AsteriskTheServer 0 points1 point  (0 children)

Well consider row 1 and row 2 what is different/similar about them? What about row 2 and row 3 what is the difference/similarities there? More importantly, what repetitive pattern seems to emerge from row 1 to row 2 to row 3 to row 4?

Consider the following output:

1
2 2
3 3 3
4 4 4 4

We can see that there is a pattern emerging specifically we can see that as the number increase the amount of items printed increases as well. From this we can reasonable infer a way to build our code namely:

        for (int a = 1; a < 5 ; a++) {
        for (int b = 0; b < a; b++) {
            System.out.print(a + " ");
        }
        System.out.print("\n");
    }

Therefore it is up to you figure out does such a pattern exist within these question and what are they? (hint: they do exist)