Signup/Sign In
PUBLISHED ON: APRIL 17, 2021

Java Program to Print the Multiplication Table in Triangular Form

In this tutorial, we will learn how to print the multiplication table in a triangular form. In this form, a table is displayed row and column-wise, in such a way that in every row, only the entries up to the same column number are filled. But before moving forward if you are not familiar with the concept of loops in java, then do check the article on Loops in Java.

Input: Enter the number of rows: 7

Output: The table in triangular form:

1 2 3 4 5 6 7

1

2 4

3 6 9

4 8 12 16

5 10 15 20 25

6 12 18 24 30 36

7 14 21 28 35 42 49

Approach:

  1. Firstly, enter the number of rows.
  2. The for(i=0; i<rows; i++) loop is used to print the column number lines.
  3. The for(i=0; i<rows; i++) loop, is used to print the n rows entries.
  4. The nested loop for(j = 0; j<=i; j++), is used to print the current entry.

Let us look at the examples to understand the implementation of the above approach.

Program 1: Print the Multiplication Table in Triangular Form

In this program, we will see how to print the multiplication table in a triangular form.

Algorithm:

  1. Start
  2. Create an instance of the BufferedReader class.
  3. Declare a variable to store the number of rows.
  4. Ask the user to initialize the variable.
  5. Use a for loop to print the first row of the table.
  6. Now use two for loops to print the multiplication table.
  7. Display the result.
  8. Stop.

The below example illustrates the implementation of the above algorithm.

//Java Program to Print the Multiplication Table in Triangular Form
import java.util.*;
  
public class Main 
{
    public static void main(String args[])
    {
        int rows, i, j;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of rows: ");
        rows = sc.nextInt();
        // Loop to print multiplication
        // table in triangular form
        for (i = 1; i <= rows; i++) 
        {
            System.out.print(i+" ");
        }
        System.out.println();
        for (i = 1; i <= rows; i++) 
        {
            for (j = 1; j <= i; j++) 
            {
                System.out.print(i * j + " ");
            }
            System.out.println();
        }
    }
}


Enter the number of rows: 6
1 2 3 4 5 6
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36

Program 2: Print the Multiplication Table in Triangular Form

In this program, we will see how to print the multiplication table in a triangular form using a try-catch block.

Algorithm:

  1. Start
  2. Create an instance of the BufferedReader class.
  3. Declare a variable to store the number of rows.
  4. Ask the user to initialize the variable.
  5. Use a try-catch block to ensure that correct input is provided by the user.
  6. Use a for loop to print the first row of the table.
  7. Now use two for loops to print the multiplication table.
  8. Display the result.
  9. Stop.

The below example illustrates the implementation of the above algorithm.

//Java Program to Print the Multiplication Table in Triangular Form
import java.io.BufferedReader;
import java.io.InputStreamReader;
 
public class Main 
{
    public static void main(String[] args) 
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int row;
        System.out.println("Enter the number of rows: ");
        try
        {
            row = Integer.parseInt(br.readLine());
        }
        catch(Exception e)
        {
            System.out.println("An error occurred");
            return;
        }
        int i,j;
        System.out.println("The table in triangular form is");
        for(i=1; i<=row; i++)
        {
            System.out.printf("%2d ",i);
        }
        System.out.println();
        for(i=1; i<=row; i++)
        {
            for(j=1; j<=i; j++)
            {
                System.out.printf("%2d ",i*j);
            }
            System.out.println();
        }
    }
}


Enter the number of rows: 8
The table in triangular form is
1 2 3 4 5 6 7 8
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.