Java Program to Print the Multiplication Table in a Triangle Form

This is the Java Program to Print the Multiplication Table in Triangular Form.

Problem Description

Write a Java Program to Print the Multiplication Table in Triangular Form. 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.

Problem Solution


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.

Program/Source Code

Here is the source code of the Java Program to Print the Multiplication Table in Triangular Form. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

  1.  
  2. //Java Program to Print the Multiplication Table in Triangular Form
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. public class TableInTriangularForm {
  8.     // Function to print tables in triangular form
  9.     public static void main(String[] args) {
  10.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  11.         int n;
  12.         System.out.println("Enter the value of n");
  13.         try{
  14.             n = Integer.parseInt(br.readLine());
  15.         }catch(Exception e){
  16.             System.out.println("An error occurred");
  17.             return;
  18.         }
  19.         int i,j;
  20.         System.out.println("The table in triangular form is");
  21.         for(i=1; i<=n; i++){
  22.             System.out.printf("%2d ",i);
  23.         }
  24.         System.out.println();
  25.         for(i=1; i<=n; i++){
  26.             for(j=1; j<=i; j++){
  27.                 System.out.printf("%2d ",i*j);
  28.             }
  29.             System.out.println();
  30.         }
  31.     }
  32. }
Program Explanation

1. In function main(), firstly the number of lines n is entered.
2. The loop for(i=1; i<=n; i++) is used to print the column number lines.
3. The loop for(i=1; i<=n; i++), is used to print the n rows entries. 4. The nested loop for(j = 1; j<=i; j++), is used to print the current entry.
Time Complexity: O(n2).

advertisement
advertisement
Runtime Test Cases
 
Case 1 (Simple Test Case):
 
Enter the value of n
10
The table in triangular form is
 1  2  3  4  5  6  7  8  9 10 
 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 
 9 18 27 36 45 54 63 72 81 
10 20 30 40 50 60 70 80 90 100

Sanfoundry Global Education & Learning Series – Java Programs..

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.