Syntax error Java Program to Print the Multiplication Table in Triangular Form

Java Program to Print the Multiplication Table in Triangular Form



In this article, we will understand how to print a multiplication table in triangular form. To print in triangular form, the table will display row and column-wise. And in every row, entries will be up to the same column number.

Below is a demonstration of the same -

Input : rows = 7
Output: 
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

Multiplication Table in a Triangular Shape

Multiplication table in triangular form is achieved using a nested loop. The following are the steps to do this.

  • Declare 3 integer variables my_input(number of lines), i(row), and j(column).

  • First, initialize the value of my_input.

  • Use a loop for(i=0; i<my_input; i++) to print the column number lines.

  • Use a loop for(i=0; i<my_input; i++) to print the number of rows value.

  • Now, use a nested loop for(j=0; j<=i; j++) to print the current value.

Example 1

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

import java.util.*;
public class MultiplicationTableTrianglePattern {
   public static void main(String args[]){
      int my_input, i, j;
      my_input = 7;
      System.out.println("The number is defined as " +my_input );
      for (i = 1; i <= my_input; i++) {
         for (j = 1; j <= i; j++) {
            System.out.print(i * j + " ");
         }
         System.out.println();
      }
   }
}

Output

The number is defined as 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

Example 2

In the following example, we accept values from the user -

import java.util.Scanner;
import java.util.*;
public class MultiplicationTableTrianglePattern {
   public static void main(String args[]){
      int my_input, i, j;
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("Enter a number ");
      my_input = my_scanner.nextInt();
      for (i = 1; i <= my_input; i++) {
         for (j = 1; j <= i; j++) {
            System.out.print(i * j + " ");
         }
         System.out.println();
      }
   }
}

Output

Enter a number
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
Manisha Chand
Manisha Chand

Words That Decode Code

Updated on: 2025-06-09T13:21:42+05:30

365 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements