This is the Java Program to Print the Multiplication Table in Triangular Form.
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.
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.
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.
//Java Program to Print the Multiplication Table in Triangular Form
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class TableInTriangularForm {
// Function to print tables in triangular form
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n;
System.out.println("Enter the value of n");
try{
n = 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<=n; i++){
System.out.printf("%2d ",i);
}
System.out.println();
for(i=1; i<=n; i++){
for(j=1; j<=i; j++){
System.out.printf("%2d ",i*j);
}
System.out.println();
}
}
}
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).
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]- Practice Programming MCQs
- Apply for Java Internship
- Apply for Computer Science Internship
- Check Java Books
- Practice BCA MCQs