-3

I'm learning Java and one of the task I've been given is to code a multiplication table that show like this:

 1  2  3  4  5  6  7  8  9 10 // 1
 2  4  6  8 10 12 14 16 18 20 // 2
 3  6  9 12 15 18 21 24 27 30 // 3
....

I'm working on it since 2 days but I can't put my finger on the answer. My main concern is how to write a code that does the multiplication to 10 and goes back to the next line for the next row.

I've tested a lot of approaches like the code below but there's a problem and I don't know where.

Please, help me.

int t = 1;
while(t <= 10) {
    int r = 1;
    int a = 1;
    int b = 1;
    System.out.print(r + " ");
    a = a + 1;
    t++;
}
Cheikh.S
  • 3
  • 4
  • 2
    For a table you will need *two* loops. Nest them. –  Aug 27 '19 at 15:18
  • Possible duplicate of [format 12 by 12 multiplication table](https://stackoverflow.com/questions/19065228/format-12-by-12-multiplication-table) – JustAnotherDeveloper Aug 27 '19 at 15:54
  • We do not have the same problem. He's looking for formatting his text eve though the answer is already there. But problem is I've done the same as he but it only displays the first line. – Cheikh.S Aug 27 '19 at 16:24
  • You haven't done the same. You have a single `while`, he has 2 `for`. This is IT science, if you write that code, you'll get the exact result of him. Just change 12 with 10 and try to understand the code. – JustAnotherDeveloper Aug 28 '19 at 07:09

4 Answers4

2

A table like this is probably best done with a for-loop:

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

If you haven't learned for loops and want to use while loops, you can use

int i = 1;
int j = 1;

while (i <= 10) {
    while (j <= 10) {
        System.out.print(i*j + " ");
        j = j + 1;
    }
    System.out.println();
    i = i + 1;
}
John W.
  • 21
  • 3
  • Thanks for your answer. I've tried it but actually it only displays the first line This is my code: ```public class Solution { public static void main(String[] args) throws Exception { //write your code here int a = 1; int b = 1; while(a <= 10){ while(b <= 10){ System.out.print(a * b + " "); b = b+1; } System.out.println(); a = a + 1; } } ``` – Cheikh.S Aug 27 '19 at 16:21
0

Try two for loops.

for(i = 1; i <= 10; i ++) {
  for(j = 1; i <= 10; i ++) {
     System.out.print(i*j + " ");
  }
  System.out.println();
}
Sam Keays
  • 736
  • 1
  • 5
  • 17
  • Not really much of a point of using `\n` over `println`, especially since `\n` does not necessarily work in all environments, while `println` uses the system property of `line.separator`. – Nexevis Aug 27 '19 at 15:41
  • Changed it, though this will have the right output. – Sam Keays Aug 28 '19 at 15:34
0

use to loop inner with the inner loop

public  void multiacation(){
for(i = 1; i <= 10; i ++) {
  for(j = 1; i <= 10; i ++) {
System.out.println(i*j +"");}}

try this and tell me in coomit if it work or not

Mohammed Al-Reai
  • 2,344
  • 14
  • 18
0

is this wat yoy need

  public class HelloWorld{

     public static void main(String []args){
        System.out.println("Hello World");
        int count = 1;
do {
  for( int j = 1; j <= 10; j ++) {
System.out.print( count*j +""+'\t');}
            count++;
            System.out.print('\n');

}while (count<11);





     }
}
Mohammed Al-Reai
  • 2,344
  • 14
  • 18