Write a Java program to print the right angled triangle number pattern using for loop.
import java.util.Scanner; public class RightTriangleNumber1 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Right Triangle Number Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Right Angled Triangle Number Pattern"); for (int i = 1 ; i <= rows; i++ ) { for (int j = 1 ; j <= i; j++ ) { System.out.printf("%d ", i); } System.out.println(); } } }
Java Program to Print Right Triangle Number Pattern
This example displays the numbers in right angled triangle pattern using a while loop
import java.util.Scanner; public class RightTriangleNumber2 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); int i = 1, j; System.out.print("Right Triangle Number Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Right Angled Triangle Number Pattern"); while (i <= rows ) { j = 1 ; while( j <= i ) { System.out.printf("%d ", i); j++; } System.out.println(); i++; } } }
Right Triangle Number Pattern Rows = 9
Right Angled Triangle Number Pattern
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9
This program helps to print the right triangle number pattern using a do while loop.
import java.util.Scanner; public class RightTriangleNumber3 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Right Triangle Number Pattern Rows = "); int rows = sc.nextInt(); System.out.println(""); int i = 1, j; do { j = 1 ; do { System.out.printf("%d ", i); } while( ++j <= i ); System.out.println(); } while (++i <= rows ); } }
Right Triangle Number Pattern Rows = 8
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
8 8 8 8 8 8 8 8