Java program to print Pyramid star pattern program. We have written the below print/draw Pyramid asterisk/star pattern program in four different ways with sample example and output, check it out. At the end of the program, we have added compiler such that you can execute the below codes.
- Print Pyramid Star Pattern – Using For Loop
- Print – Using While Loop
- Print – Using Do While Loop
Using For Loop
1) The outer for loop iterates until the condition i<=n is false. It represents rows.
2) 1st inner for loop will display space until j<n-i is false, 2nd inner loop display the character until the condition j<(i*2)-1 is false. These two loops represent the column.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import java.util.Scanner; public class PASTangle { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter N : "); int n=sc.nextInt(); System.out.print("Enter Symbol : "); char c = sc.next().charAt(0); for(int i=1;i<=n;i++) { for(int j=0;j<n-i;j++) { System.out.print(" "); } for(int j=0;j<(i*2)-1;j++) { System.out.print(c); } System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Enter N : 10 Enter Symbol : * * *** ***** ******* ********* *********** ************* *************** ***************** ******************* |
Using While Loop
1) The outer while loop executes the statements until the condition i<n is false.
2) The 1st inner while loop will display space until j++<n-i is false. The 2nd inner loop will display character until j++<(i*2)-1 is false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.util.Scanner; public class PASTangle { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter N : "); int n=sc.nextInt(); System.out.print("Enter Symbol : "); char c = sc.next().charAt(0); int i=1,j; while(i<=n) { j=0; while(j++<n-i) { System.out.print(" "); ; } j=0; while(j++<(i*2)-1) { System.out.print(c); } System.out.println(); i++; } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Enter N : 12 Enter Symbol : . . ... ..... ....... ......... ........... ............. ............... ................. ................... ..................... ....................... |
Using Do-While Loop
1) The outer do-while loop will execute the code until ++i<n is false.
2) The 1st inner do-while loop will prints space until j++<(n-i-1)is false, the 2nd inner loop will prints character until the condition j++<i*2-2 is false
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.util.Scanner; public class PASTangle { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter N : "); int n=sc.nextInt(); System.out.print("Enter Symbol : "); char c = sc.next().charAt(0); int i=1,j; do { j=0; do { System.out.print(" "); }while(j++<(n-i-1)); j=0; do { System.out.print(c); }while(j++<i*2-2); System.out.println(); } while(++i<n); ; } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 |
Enter N : 10 Enter Symbol : # # ### ##### ####### ######### ########### ############# ############### ################# |