Write a Java Program to print right angled triangle star pattern using for loop. This right triangle star example uses a nested for loop to iterate the rows and display the output.
package ShapePrograms; import java.util.Scanner; public class RightTriangle1 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Right Angled Triangle Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Right Angled Triangle Star Pattern"); for (int i = 1 ; i <= rows; i++ ) { for (int j = 1 ; j <= i; j++ ) { System.out.print("*"); } System.out.println(); } } }
In this right angled triangle star pattern program, we replaced for loops with while loops.
package ShapePrograms; import java.util.Scanner; public class RightTriangle2 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Right Angled Triangle Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Right Angled Triangle Pattern of Stars"); int i = 1, j ; while( i <= rows ) { j = 1 ; while(j <= i ) { System.out.print("*"); j++; } System.out.println(); i++; } } }
Enter Right Angled Triangle Pattern Rows = 10
Printing Right Angled Triangle Pattern of Stars
*
**
***
****
*****
******
*******
********
*********
**********
Java Program to Print Right Angled Triangle Star Pattern using do while loop
package ShapePrograms; import java.util.Scanner; public class RightTriangle3 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Right Angled Triangle Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Right Angled Triangle Pattern of Stars"); int i = 1, j ; do { j = 1 ; do { System.out.print("*"); } while(++j <= i ) ; System.out.println(); } while( ++i <= rows ); } }
Enter Right Angled Triangle Pattern Rows = 14
Printing Right Angled Triangle Pattern of Stars
*
**
***
****
*****
******
*******
********
*********
**********
***********
************
*************
**************
In this example, the RightTrianglePattern function prints the right angled triangle pattern of a given symbol.
package ShapePrograms; import java.util.Scanner; public class RightTriangle4 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Right Angled Triangle Pattern Rows = "); int rows = sc.nextInt(); System.out.print("Character for Right Angled Triangle Pattern = "); char ch = sc.next().charAt(0); System.out.println("Printing Right Angled Triangle Pattern of Stars"); RightTrianglePattern(rows, ch); } public static void RightTrianglePattern(int rows, char ch) { for (int i = 1 ; i <= rows; i++ ) { for (int j = 1 ; j <= i; j++ ) { System.out.print(ch); } System.out.println(); } } }
Enter Right Angled Triangle Pattern Rows = 17
Character for Right Angled Triangle Pattern = $
Printing Right Angled Triangle Pattern of Stars
$
$$
$$$
$$$$
$$$$$
$$$$$$
$$$$$$$
$$$$$$$$
$$$$$$$$$
$$$$$$$$$$
$$$$$$$$$$$
$$$$$$$$$$$$
$$$$$$$$$$$$$
$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$