Vedant Chandra
Vedant Chandra

Reputation: 382

Nested loop pattern not working

I wrote a class to print the pattern:

1
2   4
3   6   9
4   8   12  16
5   10  15  20  25

The code is:

public class pat2
{
    public void method()
    {
        int row = 1;
        int val = 0;

        for(row=1;row<=5;row++)
        {
            for(val=1;val<=row;val=row*val)
            {
                System.out.print(val);
            }

            System.out.println();
        }
    }
}

I figured out that the relation between row and val is that val = row*val. Using this logic, I wrote the nested loop. However, I have not gotten the desired output, and have instead got an output of infinite 1's. I'm positive that my problem is in the wording of the second for loop, could I have help identifying it?

Upvotes: 0

Views: 399

Answers (7)

Mengjun
Mengjun

Reputation: 3197

In you second for-loop, for(val=1;val<=row;val=row*val).

val=row*val make the code be in infinite loop, it will not end.

You should use the following code, like,

public void method() {
    int row = 1;
    int val = 0;

    for (row = 1; row <= 5; row++) {
        for (val = 1; val <= row; val++) {
            System.out.printf("%2d ", row * val);
        }
        System.out.println();
    }
}

Output in console is as follows:

1 
2  4 
3  6  9 
4  8 12 16 
5 10 15 20 25 

Upvotes: 1

Alex
Alex

Reputation: 1110

Using val=val*row in your for loop is a big misunderstanding of how for-loops should be used. You want your loop counter to be incremented with a consistent count. If you trace out your current solution on paper, you will see that val will always be 1. Therefore, it will never leave the loop, and it will always print out "1"

You can fix your code as such:

public class pat2
{
    public void method()
    {
        for(int row=1; row<=5; row++)
        {
            for(int column=1; column<=row; column++)
            {
                System.out.print(row * column);
            }
            System.out.println();
        }
    }
}

Upvotes: 0

SpiderShlong
SpiderShlong

Reputation: 224

so your issue is that you say:

for(val=1;val<=row;val=row*val)

Consider what you are saying here.

Val = 1.

for as long as val <= row

val = row*val.

so this should break if value = any number greater than 1.

try this:

public void method()
{
    for(int row=1; row<=5; row++)
    {
        for(int val=1; val<=row; val++)
        {
            System.out.print(val * row);
        }

        System.out.println();
    }
}

Upvotes: 0

AJMansfield
AJMansfield

Reputation: 4175

public class Pat2 { //class names start with a capital letter
    public void method(){
        for(int row = 1; row <= 5; row++){
            for(int col = 1; col <= row; col++)
                System.out.print(row*col + "\t");
            System.out.println();
        }
    }
}

Upvotes: 1

M Abbas
M Abbas

Reputation: 6479

You only need to replace the second loop with:

for(val = 1; val <= row; val++) {
    System.out.print( val * row + "\t" );
}

Upvotes: 1

DontVoteMeDown
DontVoteMeDown

Reputation: 21465

You can't do this val=row*val on your incrment phase. This will overflow all expected algorithm bounds.

I believe that this is what you want:

for(val=1;val<=row;val++)
{
    System.out.print(val*row);
}

Upvotes: 1

Eric Barr
Eric Barr

Reputation: 443

You're close. The inner loop you do want to increment your value by row, but you don't want to increment the loop counter that much. I renamed your "val" variable to "col" and I think it's more clear what is happening:

public class pat2
{
    public void method()
    {
        int row = 1;
        int col = 1;

        for(row=1;row<=5;row++)
        {
            for(col=1;col<=row;++col)
            {
                System.out.print(col*row);
            }

            System.out.println();
        }
    }
}

Upvotes: 0

Related Questions