2

user input

5

Required output (using while loop)

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

Code:

while (temp1 <= a) {
    while (temp2 <= a) {
        temp = temp2 * temp1;
        System.out.print(temp + " ");
        temp2++;
    }
    temp1++;
    System.out.println();
}

i am taking a as input and try to form that figure but i can't.. please help

4
  • 1
    How is it not working? Is the output wrong? Commented Oct 31, 2013 at 19:46
  • Please post the entire function, this is only part of it. i.e. you are assigning stuff to temp, but temp isn't declared Commented Oct 31, 2013 at 19:46
  • 4
    Why are you not using for loops? Or your debugger to debug your code? I suspect temp2 should be reset to 1 somewhere. Commented Oct 31, 2013 at 19:46
  • 1
    I don't know who downvoted this, but he gave input, expected output, and the required code. Only thing he's missing is the actual output. Commented Oct 31, 2013 at 19:56

6 Answers 6

3

Value of temp2 after inner while loop will be a+1. Since you not resetting it to 1 later you will not enter this inner loop again because condition while(temp2<=a) will not be fulfilled. To correct it set temp2 to 1 inside outer loop, before or after inner loop.

Sign up to request clarification or add additional context in comments.

Comments

2

If I have to break down the problem into simple words,

  • The user will input a number: "x".
  • Create a 2D matrix of the size arr[x][x]
  • the value of each element will be a[i][j] = i * j; // considering (i=1; i<=x) & (j=1;j<=x)
  • Print out the matrix.

There are n number of ways you can do it.

I guess using for loops would be the simplest.

Comments

1

Code comment below explains what is wrong with your code.

//assume temp1 equals 1
while(temp1 <= a){
    temp2 = 1;//you're primarily forgetting to reset the temp2 count
    while(temp2 <= a){
        temp = temp1*temp2;
        System.out.print(temp + " ");
        temp2++;
    }
    temp1++;
    System.out.println();
}

Comments

1
    int a = 5; //Or however else you get this value.

    //Initialize your values
    int temp1 = 1;
    int temp2 = 1;
    int temp; //Only need a declaration here.

    while (temp1 <= a) {            
        while(temp2 <= a) {
            temp = temp1*temp2;
            System.out.print(temp + " ");
            temp1++;
            temp2++;
        }
        //This executes between inner loops
        temp2 = 1; //It's important to reset 
        System.out.println();
    }

Or another compact way:

    int a = 5;

    int row = 0;
    int col = 0;

    while (++row <= a) {            
        while(++col <= a) {
            System.out.print(row*col + " ");
        }
        col = 0;
        System.out.println();
    }

Comments

1
    for(int i=1; i<=a; i++){
        System.out.print(i);
        for(int j=2; j<=a; j++){
            int val = i*j;
            System.out.print(" " + val);
        }
        System.out.println();
    }

Comments

1
    int a = 5;
    int temp1 = 1;
    int temp2= 1;
    int temp = 1;

    while(temp1 <= a){
        while(temp2 <= a){
            temp = temp2*temp1;
            System.out.print(temp + " ");
            temp2++;
        }
        System.out.println();
        temp1++;
        temp2=1;
    }

The code above should have your desired results. Reset the temp2 variable at the end of the loop. Just change int a = 5 to whatever you want.

Additional Answer:

    int userInput = 5;
    int answer = 0;
    for(int y = 0; y < userInput; y++){

        for(int x = 0; x < userInput; x++ ){

            answer = x * y;
            System.out.print(answer + " ");
        }
        System.out.println();
    }

With this answer you do not need to reset the temp variable and will produce the desired results

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.