In this Java pattern program, we show the steps to print the right angled triangle pattern of multiplication numbers in every row using for loop, while loop, and functions.
The below program accepts the user-entered rows and uses the nested for loop to traverse the rows and columns. Next, the program will print the multiplication numbers in a right angled triangle pattern.
import java.util.Scanner;
public class Example {
private static Scanner sc;
public static void main(String[] args)
{
sc = new Scanner(System.in);
System.out.print("Enter Numbers of Rows = ");
int rows = sc.nextInt();
for (int i = 1 ; i <= rows; i++ )
{
for (int j = 1 ; j <= i; j++ )
{
System.out.printf("%d ", i * j);
}
System.out.println();
}
}
}
Enter Numbers of Rows = 13
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
11 22 33 44 55 66 77 88 99 110 121
12 24 36 48 60 72 84 96 108 120 132 144
13 26 39 52 65 78 91 104 117 130 143 156 169
Within this program, we replaced the for loop with a while loop to traverse the rows and columns and print the multiplication numbers in a right angled triangle pattern shape. For more Number pattern programs, please click here.
import java.util.Scanner;
public class Example {
private static Scanner sc;
public static void main(String[] args)
{
sc = new Scanner(System.in);
int rows, i, j;
System.out.print("Enter Numbers of Rows = ");
rows = sc.nextInt();
i = 1 ;
while ( i <= rows )
{
j = 1 ;
while ( j <= i )
{
System.out.printf("%d ", i * j);
j++;
}
System.out.println();
i++;
}
}
}
Enter Numbers of Rows = 12
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
11 22 33 44 55 66 77 88 99 110 121
12 24 36 48 60 72 84 96 108 120 132 144
In this Java pattern program, we created a RightTriMultiplicationNums function to print the right angled triangle of multiplication numbers on each row.
import java.util.Scanner;
public class Example {
private static Scanner sc;
public static void main(String[] args)
{
sc = new Scanner(System.in);
System.out.print("Enter Numbers of Rows = ");
int rows = sc.nextInt();
RightTriMultiplicationNums(rows);
}
public static void RightTriMultiplicationNums(int rows)
{
for (int i = 1 ; i <= rows; i++ )
{
for (int j = 1 ; j <= i; j++ )
{
System.out.printf("%d ", i * j);
}
System.out.println();
}
}
}