Write a Java program to print right pascals star triangle using for loop.
package ShapePrograms2; import java.util.Scanner; public class RightPascal1 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); int i, j; System.out.print("Enter Right Pascals Triangle Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Right Pascals Triangle Star Pattern"); for (i = 0 ; i < rows; i++ ) { for (j = 0 ; j <= i; j++ ) { System.out.print("* "); } System.out.println(); } for (i = rows - 1; i >= 0; i-- ) { for (j = 0 ; j <= i - 1; j++ ) { System.out.print("* "); } System.out.println(); } } }
This Java example displays the stars in the right pascals triangle pattern using a while loop.
package ShapePrograms2; import java.util.Scanner; public class RightPascal2 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Right Pascals Triangle Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Right Pascals Triangle Star Pattern"); int j, i = 0; while(i < rows ) { j = 0 ; while( j <= i ) { System.out.print("* "); j++; } System.out.println(); i++; } i = rows - 1; while (i >= 0) { j = 0 ; while ( j <= i - 1 ) { System.out.print("* "); j++; } System.out.println(); i--; } } }
Enter Right Pascals Triangle Pattern Rows = 8
Printing Right Pascals Triangle Star Pattern
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
Java program to print right pascals star triangle using do while loop.
package ShapePrograms2; import java.util.Scanner; public class RightPascal3 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Right Pascals Triangle Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Right Pascals Triangle Star Pattern"); int j, i = 0; do { j = 0 ; do { System.out.print("* "); } while( ++j <= i ); System.out.println(); } while(++i < rows ); i = rows - 1; do { j = 0 ; do { System.out.print("* "); } while ( ++j <= i - 1 ); System.out.println(); } while (--i > 0); } }
Enter Right Pascals Triangle Pattern Rows = 9
Printing Right Pascals Triangle Star Pattern
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
In this Java example, the RightPascalTrianglePattern function prints a given symbol’s right pascal triangle pattern.
package ShapePrograms2; import java.util.Scanner; public class RightPascal4 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Right Pascals Triangle Pattern Rows = "); int rows = sc.nextInt(); System.out.print("Enter Character for Right Pascals Triangle Pattern = "); char ch = sc.next().charAt(0); System.out.println("Printing Right Pascals Triangle Pattern"); RightPascalTrianglePattern(rows, ch); } public static void RightPascalTrianglePattern(int rows, char ch) { int i, j; for (i = 0 ; i < rows; i++ ) { for (j = 0 ; j <= i; j++ ) { System.out.print(ch + " "); } System.out.println(); } for (i = rows - 1; i >= 0; i-- ) { for (j = 0 ; j <= i - 1; j++ ) { System.out.print(ch + " "); } System.out.println(); } } }
Enter Right Pascals Triangle Pattern Rows = 11
Enter Character for Right Pascals Triangle Pattern = #
Printing Right Pascals Triangle Pattern
#
# #
# # #
# # # #
# # # # #
# # # # # #
# # # # # # #
# # # # # # # #
# # # # # # # # #
# # # # # # # # # #
# # # # # # # # # # #
# # # # # # # # # #
# # # # # # # # #
# # # # # # # #
# # # # # # #
# # # # # #
# # # # #
# # # #
# # #
# #
#