How to Print Table in Java?

In this section, we will understand the logic to print tables and also implement that logic in Java programs.

A table (or multiplication table) is a sequence of numbers that are generated using multiplication. We enter an integer as input of which we want to print the table. After that, we multiply the entered number by 1 to 10, respectively on by one.

Suppose, we have entered 5 as input then the table of 5 will be:

5*1 = 5
5*2 = 10
5*3 = 15
------------
------------
5*8 = 40
5*9 = 45
5*10 = 50

We observe that the entered number is fixed and a variable is changing at each iteration. Let's implement the above logic in a Java program.

Printing Table in Java

There are many ways to print table in Java that are as follows:

  • Using Java for Loop
  • Using Java while Loop

Using Java for Loop

In the following program, we have entered a number whose table is to be print. After that, we have used a for loop that starts from 1 and executes till the condition i<=10 becomes false. Inside the for loop, we have performed the multiplication and print the same.

TableExample.java

Output:

How to Print Table in Java

Using Java while Loop

The logic to print the table is the same as we have used in the previous program. But in the following program, we have used Java while loop in place of a for loop.

PrintTable.java

Output:

How to Print Table in Java

In the above program, we can also use the following statement to print the table:


Next TopicJava Create PDF




Latest Courses