Skip to content
Related Articles

Related Articles

Java Program to Print the Multiplication Table in a Triangle Form
  • Last Updated : 04 Dec, 2020

In this form, a table is displayed row and column-wise, in such a way such that in every row, only the entries up to the same column number filled.

Example:

Input : rows = 6
Output:
    1
    2 4
    3 6 9
    4 8 12 16
    5 10 15 20 25
    6 12 18 24 30 36  

Approach: The idea is to use nested loops. First, display the column numbers. Then, use to a nested loop to fill out the entries of the row.

  1. In function main(), firstly the number of lines n is entered.
  2. The loop for(i=0; i<rows; i++) is used to print the column number lines.
  3. The loop for(i=0; i<rows; i++), 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.

Below is the implementation of the above approach.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to Print the Multiplication
// Table in Triangular Form
  
import java.util.*;
  
public class MultiplicationTableTrianglePattern {
  
    // Function to print tables in triangular form
  
    public static void main(String args[])
    {
        int rows, i, j;
  
        Scanner in = new Scanner(System.in);
  
        rows = 6;
  
        // Loop to print multipliacation
        // table in triangular form
  
        for (i = 1; i <= rows; i++) {
            for (j = 1; j <= i; j++) {
                System.out.print(i * j + " ");
            }
            System.out.println();
        }
    }
}

chevron_right


Output

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

Time Complexity: O(n2), where n is a number of rows.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :