user12392110
user12392110

Reputation:

Multiplication Array table

I need to get this result : input I wanted to have

I am very close but the final step is killing me:

public static void printMultiplicationTable(int size) {
    System.out.print("\t");
    for (int i = 0; i <= size; i++) {
        for (int j = 0; j <= size; j++) {
            if (j == 0) System.out.print( i );
            else if (i == 0) System.out.print("\t" + j);
            else System.out.print("\t" + i * j);
        }
        System.out.println();
    }
}

How do I get empty space at first line and start from 1 instead of 0 ?

Upvotes: 1

Views: 89

Answers (2)

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48751

Your "one loop to rule them all" is making this more difficult for you.

Break it down into simpler concepts.

  1. Print the header row first.
  2. Print the rows and check for the first index to print the left-side
  3. Pre-calculate the width of the max value (for formatting)
public class MathTutor {
    public static void main(String[] args) {
        printMultiplicationTable(3);
        printMultiplicationTable(6);
        printMultiplicationTable(12);
    }

    public static void printMultiplicationTable(int size) {
        String numberFormat = String.format("%%%dd", maxDigits(size));
        for (int col = 0; col < size; col++) {
            System.out.print("\t");
            System.out.printf(numberFormat, col + 1);
        }
        System.out.println();
        for (int row = 1; row <= size; row++) {
            System.out.printf(numberFormat, row);
            for (int col = 1; col <= size; col++) {
                System.out.print("\t");
                System.out.printf(numberFormat, row * col);
            }
            System.out.println();
        }
    }

    public static int maxDigits(int size) {
        return (int) Math.floor(Math.log10(Math.pow(size, 2))) + 1;
    }
}

Multiplication table for 3

    1   2   3
1   1   2   3
2   2   4   6
3   3   6   9

Multiplication table for 6

     1   2   3   4   5   6
 1   1   2   3   4   5   6
 2   2   4   6   8  10  12
 3   3   6   9  12  15  18
 4   4   8  12  16  20  24
 5   5  10  15  20  25  30
 6   6  12  18  24  30  36

Multiplication table for 12

      1   2   3   4   5   6   7   8   9  10  11  12
  1   1   2   3   4   5   6   7   8   9  10  11  12
  2   2   4   6   8  10  12  14  16  18  20  22  24
  3   3   6   9  12  15  18  21  24  27  30  33  36
  4   4   8  12  16  20  24  28  32  36  40  44  48
  5   5  10  15  20  25  30  35  40  45  50  55  60
  6   6  12  18  24  30  36  42  48  54  60  66  72
  7   7  14  21  28  35  42  49  56  63  70  77  84
  8   8  16  24  32  40  48  56  64  72  80  88  96
  9   9  18  27  36  45  54  63  72  81  90  99 108
 10  10  20  30  40  50  60  70  80  90 100 110 120
 11  11  22  33  44  55  66  77  88  99 110 121 132
 12  12  24  36  48  60  72  84  96 108 120 132 144

Upvotes: 0

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79620

You were indeed close. your first System.out.print("\t"); was causing extra tab. Also, I have added the output for the condition when i==0 && j==0. Given below is the program that prints the desired result:

public class TestMyClass {
    public static void main(String[] args) {
        printMultiplicationTable(4);
    }

    public static void printMultiplicationTable(int size) {
        for (int i = 0; i <= size; i++) {
            for (int j = 0; j <= size; j++) {
                if (i==0 && j==0)
                    System.out.print("");
                else if (j == 0)
                    System.out.print(i);
                else if (i == 0)
                    System.out.print("\t" + j);
                else
                    System.out.print("\t" + i * j);
            }
            System.out.println();
        }
    }
}

Output:

    1   2   3   4
1   1   2   3   4
2   2   4   6   8
3   3   6   9   12
4   4   8   12  16

Upvotes: 1

Related Questions