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.
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.
- In function main(), firstly the number of lines n is entered.
- The loop for(i=0; i<rows; i++) is used to print the column number lines.
- 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.
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.