Creating Multiplication Table in Java

A multiplication table is a table of numbers which lists product of a decimal sequence of numbers. For example, a multiplication table of 9 by 9 contains a top most row with values ranging from 1 to 9 and a left most row with values ranging from 1 to 9. The middle cells contains the algebraic product of the corresponding value in the top row and left most row. Following is a 5 by 5 multiplication table,

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

Using a multiplication table it is easy to find product of two numbers. For example, to find the product of 4 and 5, we just need to look at the cell located on the 4th row and 5th column which returns the result as 20.

It is important to memorize multiplication tables as it the foundation of mathematical calculations. Children are usually taught to memorize multiplication tables up to 9 by 9. This enables children to do multiply large numbers without the need of a calculator.

The technique of memorizing the table was to take one column at a time and then memorize it. For example, the multiplication table for number 7 is memorized as,

1 x 7 7
2 x 7 14
3 x 7 21
4 x 7 28
5 x 7 35
6 x 7 42
7 x 7 49
8 x 7 56
9 x 7 63
10 x 7 70

 

Java Program to Print a 9 by 9 Multiplication Table

The following Java program generates the entire multiplication table for values from 1 to 9. This is printed in a formatted table. We use nested loops to generate the multiplication table. Also note the use of System.out.format() method to format the table. The format string %4d instructs the formatter to print the integer in 4 character width padding with spaces as necessary.

/* Prints multiplication table in Java */
public class FullMultiplicationTable {
    
    public static void main(String[] args) {
        int tableSize = 9;
        printMultiplicationTable(tableSize);
    }
    
    public static void printMultiplicationTable(int tableSize) {
        // first print the top header row
        System.out.format("      ");
        for(int i = 1; i<=tableSize;i++ ) {
            System.out.format("%4d",i);
        }
        System.out.println();
        System.out.println("------------------------------------------");
        
        for(int i = 1 ;i<=tableSize;i++) {
            // print left most column first
            System.out.format("%4d |",i);
            for(int j=1;j<=tableSize;j++) {
                System.out.format("%4d",i*j);
            }
            System.out.println();
        }
    }
    
}

The output generated by the multiplication table program in java is given below,

A 9 by 9 multiplication table in Java

Java Program to Print Multiplication Table for an Integer

The following program generates multiplication table for an integer. The table contains product values for the integer when it is multiplied with values ranging from 1 to 10. This form is usually used to memorize the multiplication table,

/* Generates multiplication table for an integer */
public class MultiplicationTable {

    public static void main(String[] args) {
        int number = 7;
        
        printMultiplicationTable(number);
    }

    private static void printMultiplicationTable(int n) {
       System.out.println("Multiplication table for "+n);
       System.out.println("---------------------------");
       for(int i = 1; i<=10;i++) {
           System.out.format("%2d x %d = %3d\n", i,n,i*n);
       }
    }
    
}

The formatted multiplication table output is given below,

Multiplication table for 7

Do you have a programming problem that you are unable to solve? Please send us your problem and we will publish the solution on this site within 48 hours! Please email us.


Leave a Reply