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