In this article, we will learn to print the different Star Pattern Programs in Java. This is one of the popular Java pattern program interview question for fresher. The pattern program are the most recommended programs to enhance the logical thinking and for the better understanding of flow control. Lets look into the below possible Star Pattern Programs in Java which might help for both the fr
Star Pattern Programs In Java
Pattern 1:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern1 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); // Print i number of stars for (int i=1; i<=rows; i++) { for (int j=1; j<=i; j++) { System.out.print("*"); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * ** *** **** *****
Pattern 2:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern2 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i=1; i<=rows; i++) { // Print space in decreasing order for (int j=rows; j>i; j--) { System.out.print(" "); } // Print star in increasing order for (int k=1; k<=i; k++) { System.out.print("*"); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * ** *** **** *****
Pattern 3:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern3 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i=1; i<=rows; i++) { // Print star in decreasing order for (int k=rows; k>=i; k--) { System.out.print("*"); } // Print space in increasing order for (int j=1; j<i; j++) { System.out.print(" "); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## ***** **** *** ** *
Pattern 4:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern4 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i=1; i<=rows; i++) { // Print space in increasing order for (int j=1; j<i; j++) { System.out.print(" "); } // Print star in decreasing order for (int k=rows; k>=i; k--) { System.out.print("*"); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## ***** **** *** ** *
Pattern 5: [ Pyramid Star Pattern ]
package com.javainterviewpoint; import java.util.Scanner; public class Pattern5 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i=1; i<=rows; i++) { // Print space in decreasing order for (int j=rows; j>i; j--) { System.out.print(" "); } // Print star in increasing order for (int k=1; k<=(i * 2) -1; k++) { System.out.print("*"); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * *** ***** ******* *********
Pattern 6:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern6 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i=rows; i>=1; i--) { // Print star in decreasing order for (int k=1; k<=(i * 2) -1; k++) { System.out.print("*"); } System.out.println(); // Print space in increasing order for (int j=rows; j>=i; j--) { System.out.print(" "); } } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## ********* ******* ***** *** *
Pattern 7: [Diamond Star Pattern ]
package com.javainterviewpoint; import java.util.Scanner; public class Pattern7 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i=1; i<=rows; i++) { // Print space in decreasing order for (int j=rows; j>i; j--) { System.out.print(" "); } // Print star in increasing order for (int k=1; k<=(i * 2) -1; k++) { System.out.print("*"); } System.out.println(); } for (int i=rows-1; i>=1; i--) { // Print space in increasing order for (int j=rows-1; j>=i; j--) { System.out.print(" "); } // Print star in decreasing order for (int k=1; k<=(i * 2) -1; k++) { System.out.print("*"); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * *** ***** ******* ********* ******* ***** *** *
Pattern 8:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern8 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); // Print i number of stars for (int i=1; i<=rows; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); } for (int i=1; i<=rows-1; i++) { // Print star in decreasing order for (int j = rows-1; j >= i; j--) { System.out.print("*"); } // Print space in increasing order for (int k = 1; k < i; k++) { System.out.print(" "); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * ** *** **** ***** **** *** ** *
Pattern 9:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern9 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i = 1; i <= rows; i++) { // Print space in decreasing order for (int j = rows; j > i; j--) { System.out.print(" "); } // Print star in increasing order for (int k = 1; k <= i; k++) { System.out.print("*"); } System.out.println(); } for (int i = 1; i <= rows-1; i++) { // Print space in increasing order for (int j = 1; j <= i; j++) { System.out.print(" "); } // Print star in decreasing order for (int k = rows-1; k >= i; k--) { System.out.print("*"); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * ** *** **** ***** **** *** ** *
Pattern 10:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern10 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i = 1; i <= rows; i++) { for (int j = rows-1; j>=i; j--) { System.out.print(" "); } // Print star in decreasing order for (int k = 1; k <= rows; k++) { System.out.print("*"); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## ***** ***** ***** ***** *****
Pattern 11:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern11 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i-1; j++) { System.out.print(" "); } // Print star in decreasing order for (int k = 1; k <= rows; k++) { System.out.print("*"); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## ***** ***** ***** ***** *****
Pattern 12:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern12 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i = rows; i >= 1; i--) { for (int j = i; j >= 1; j--) { System.out.print("*"); } System.out.println(); } for (int i = 2; i <= rows; i++) { for (int j = i; j >= 1; j--) { System.out.print("*"); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## ***** **** *** ** * ** *** **** *****
Pattern 13:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern13 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i = 1; i <= rows; i++) { for (int j = 1; j < i; j++) { System.out.print(" "); } for (int k = i; k <= rows; k++) { System.out.print("*"); } System.out.println(); } for (int i = rows-1; i >= 1; i--) { for (int j = 2; j <=i; j++) { System.out.print(" "); } for (int k = i; k <= rows; k++) { System.out.print("*"); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## ***** **** *** ** * ** *** **** *****
Pattern 14:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern14 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i = 1; i <= rows; i++) { for (int j = 1; j < i; j++) { System.out.print(" "); } for (int k = i; k <= rows; k++) { System.out.print("* "); } System.out.println(); } for (int i = rows-1; i >= 1; i--) { for (int j = 1; j < i; j++) { System.out.print(" "); } for (int k = i; k <= rows; k++) { System.out.print("* "); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Pattern 15:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern15 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); // Print i number of stars for (int i=1; i<=rows; i++) { for (int j=1; j<=i; j++) { if( j == 1 || j == i || i == rows) System.out.print("*"); else System.out.print(" "); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * ** * * * * *****
Pattern 16:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern16 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i=1; i<=rows; i++) { // Print space in decreasing order for (int j=rows; j>i; j--) { System.out.print(" "); } // Print star in increasing order for (int k=1; k<=i; k++) { if( k == 1 || k == i || i == rows) System.out.print("*"); else System.out.print(" "); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * ** * * * * *****
Pattern 17:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern17 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i=1; i<=rows; i++) { // Print star in decreasing order for (int j=rows; j >=i; j--) { if( i == 1 || j == i || j == rows) System.out.print("*"); else System.out.print(" "); } // Print space in increasing order for (int k=1; k<i; k++) { System.out.print(" "); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## ***** * * * * ** *
Pattern 18:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern18 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i=1; i<=rows; i++) { // Print space in increasing order for (int j=1; j<i; j++) { System.out.print(" "); } // Print star in decreasing order for (int k=rows; k>=i; k--) { if( i == 1 || k == i || k == rows) System.out.print("*"); else System.out.print(" "); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## ***** * * * * ** *
Pattern 19:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern19 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i=1; i<=rows; i++) { // Print space in decreasing order for (int j=rows; j>i; j--) { System.out.print(" "); } // Print star in increasing order for (int k=1; k<=(i * 2) -1; k++) { if( k == 1 || k == (i * 2) -1 || i == rows) System.out.print("*"); else System.out.print(" "); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * * * * * * * *********
Pattern 20:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern20 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i=rows; i>=1; i--) { // Print star in decreasing order for (int j=1; j <=(i * 2) -1; j++) { if( j == 1 || j == (i * 2) -1 || i == rows) System.out.print("*"); else System.out.print(" "); } System.out.println(); // Print space in increasing order for (int k = rows; k >= i; k--) { System.out.print(" "); } } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## ********* * * * * * * *
Pattern 21:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern21 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i=1; i<=rows; i++) { // Print space in decreasing order for (int j=rows; j>i; j--) { System.out.print(" "); } // Print star in increasing order for (int k=1; k<=(i * 2) -1; k++) { if( k == 1 || k == (i * 2) -1) System.out.print("*"); else System.out.print(" "); } System.out.println(); } for (int i=rows-1; i>=1; i--) { // Print space in increasing order for (int j=rows-1; j>=i; j--) { System.out.print(" "); } // Print star in decreasing order for (int k=1; k<=(i * 2) -1; k++) { if( k == 1 || k == (i * 2) -1 ) System.out.print("*"); else System.out.print(" "); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * * * * * * * * * * * * * * * *
Pattern 22:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern22 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); // Print i number of stars for (int i=1; i<=rows; i++) { for (int j=1; j<=i; j++) { if( j == 1 || j == i ) System.out.print("*"); else System.out.print(" "); } System.out.println(); } for (int i=1; i<=rows-1; i++) { // Print star in decreasing order for (int j = rows-1; j >= i; j--) { if( j == rows-1 || j == i || i == rows) System.out.print("*"); else System.out.print(" "); } // Print space in increasing order for (int k = 1; k < i; k++) { System.out.print(" "); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * ** * * * * * * * * * * ** *
Pattern 23:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern23 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i = 1; i <= rows; i++) { // Print space in decreasing order for (int j = rows; j > i; j--) { System.out.print(" "); } // Print star in increasing order for (int k = 1; k <= i; k++) { if( k == 1 || k == i ) System.out.print("*"); else System.out.print(" "); } System.out.println(); } for (int i = 1; i <= rows-1; i++) { // Print space in increasing order for (int j = 1; j <= i; j++) { System.out.print(" "); } // Print star in decreasing order for (int k = rows-1; k >= i; k--) { if( k == rows-1 || k == i ) System.out.print("*"); else System.out.print(" "); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * ** * * * * * * * * * * ** *
Pattern 24:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern24 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i = 1; i <= rows; i++) { for (int j = rows-1; j>=i; j--) { System.out.print(" "); } // Print star in decreasing order for (int k = 1; k <= rows; k++) { if( i == 1 || i == rows || k == 1 || k == rows ) System.out.print("*"); else System.out.print(" "); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## ***** * * * * * * *****
Pattern 25:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern25 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i-1; j++) { System.out.print(" "); } // Print star in decreasing order for (int k = 1; k <= rows; k++) { if( i == 1 || i == rows || k == 1 || k == rows ) System.out.print("*"); else System.out.print(" "); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## ***** * * * * * * *****
Pattern 26:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern26 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); // Print i number of stars for (int i=1; i<=rows; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } for (int k = i*2; k <= rows*2-1; k++) { System.out.print(" "); } for (int l = 1; l <= i; l++) { System.out.print("*"); } System.out.println(); } for (int i=1; i<=rows-1; i++) { for (int j = rows-1; j >= i; j--) { System.out.print("*"); } for (int k = 1; k <= i*2; k++) { System.out.print(" "); } for (int l = rows-1; l >= i; l--) { System.out.print("*"); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * * ** ** *** *** **** **** ********** **** **** *** *** ** ** * *
Pattern 27:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern27 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); // Print i number of stars for (int i=1; i<=rows; i++) { for (int j = i; j <= rows; j++) { System.out.print("*"); } for (int k = 1; k <= i*2-2; k++) { System.out.print(" "); } for (int l = i; l <= rows; l++) { System.out.print("*"); } System.out.println(); } for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } for (int k = i*2-2; k < rows*2-2; k++) { System.out.print(" "); } for (int l = 1; l <= i; l++) { System.out.print("*"); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## ********** **** **** *** *** ** ** * * * * ** ** *** *** **** **** **********
Pattern 28:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern28 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i=1; i<=rows; i++) { for (int j=rows; j>i; j--) { System.out.print(" "); } for (int k=1; k<=(i * 2) -1; k++) { if(k == 1 || k == i*2 -1 || k == 1) System.out.print("*"); else System.out.print(" "); } System.out.println(); } for (int i=rows-1; i>=1; i--) { for (int j=rows-1; j>=i; j--) { System.out.print(" "); } for (int k=1; k<=(i * 2) -1; k++) { if(k == 1 || k == i*2 -1 || k == 1) System.out.print("*"); else System.out.print(" "); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * * * * * * * * * * * * * * * *
Pattern 29:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern29 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i = 1; i <= rows; i++) { for (int j = 1; j < i; j++) { System.out.print(" "); } for (int k = i; k <= rows; k++) { if( i == 1 || k == i || k == rows) System.out.print("* "); else System.out.print(" "); } System.out.println(); } for (int i = rows-1; i >= 1; i--) { for (int j = 1; j < i; j++) { System.out.print(" "); } for (int k = i; k <= rows; k++) { if( i == 1 || k == i || k == rows) System.out.print("* "); else System.out.print(" "); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * * * * * * * * * * * * * * * * * * * * * * *
Pattern 30:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern30 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); // Print i number of stars for (int i=1; i<=rows; i++) { for (int j=1; j<=rows; j++) { System.out.print("*"); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## ***** ***** ***** ***** *****
Pattern 31:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern31 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); // Print i number of stars for (int i=1; i<=rows; i++) { for (int j=1; j<=rows; j++) { if(i ==1 || i == rows || j == 1 || j == rows ) System.out.print("*"); else System.out.print(" "); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## ***** * * * * * * *****
Pattern 32:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern32 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to in the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); // Print i number of stars for (int i=1; i<=(rows * 2 -1); i++) { for (int j=1; j<=rows; j++) { if(j==i || j==rows-i+1) { System.out.print("*"); } System.out.print(" "); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to in the pattern 5 ## Printing the pattern ## * * * * * * * * *
Pattern 33:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern33 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to in the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); // Print i number of stars for (int i=1; i<=(rows * 2 -1); i++) { if( i == rows) { // Printing Horizontal Line of Stars for (int j=1; j<=(rows * 2 -1); j++) { System.out.print("*"); } } else { // Printing space before Vertical Line of Stars for(int k=1; k<= rows-1; k++) { System.out.print(" "); } System.out.print("*"); } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to in the pattern 5 ## Printing the pattern ## * * * * ********* * * * *
Pattern 34:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern34 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to in the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); // Print i number of stars for (int i = 1; i <= rows * 2 - 1; i++) { if (i == 1 || i == rows || i == rows * 2 - 1) { for (int j = 1; j <= rows; j++) { if (j == 1 || j == rows) System.out.print(" "); else System.out.print("*"); } } else { for (int k = 1; k <= rows; k++) { if (k == 1 || k == rows) System.out.print("*"); else System.out.print(" "); } } System.out.println(); } scanner.close(); } }
Output
Enter the number of rows needed to in the pattern 5 ## Printing the pattern ## *** * * * * * * *** * * * * * * ***
Pattern 35:
package com.javainterviewpoint; import java.util.Scanner; public class Pattern35 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to in the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); // Print i number of stars for(int i=1; i<= rows; i++) { if(i%2 != 0) { for(int j=1; j<= rows/2+1; j++) { System.out.print("* "); } } else { for(int j=1; j<= rows/2; j++) { System.out.print(" * "); } } System.out.println(""); } } }
Output
Enter the number of rows needed to in the pattern 5 ## Printing the pattern ## * * * * * * * * * * * * *
Raj says
**
*
****(4*)
*
********(8*)
*
****************(16*)
Code for this
javainterviewpoint says
Here you go
Parikshit Naskar says
I have star problem. Please solve the problem
-*-*-*-*
–*-*-*-
-*-*-*-*
–*-*-*-
-*-*-*-*
–*-*-*-
javainterviewpoint says
Here you go
sunil says
*#######
######**
***#####
####****
****####
#####***
**######
#######*
javainterviewpoint says
Here you go
carol denvers says
* * * * *
* * * *
* *
* * * *
* * * * *
javainterviewpoint says
Here you go
Aaaaaa says
Give me answers to this star output
_ _ _ _ *
_ _ _***
_ _*****
_*******
*********
*********
*********
*********
*********
javainterviewpoint says
Here you go
irteza khan says
i have a star problem as follows with some lines printed in between. solve this
*_*_*_*_*_*_*_*
*_*_*_*_*_*_*
*_*_*_*_*_*
*_*_*_*_*
*_*_*_*
*_*_*
*_*
*
welcome to java
welcome to computer science
programming is fun
*_*_*_*_*_*_*_*
*_*_*_*_*_*_*
*_*_*_*_*_*
*_*_*_*_*
*_*_*_*
*_*_*
*_*
*
javainterviewpoint says
Here you go
Pattern1
Pattern2
Puneet says
I have a pattern problem can you solve this:
1.Condition: N=1
Output: *
2.Condition: N=5
Output: *####
###**
***##
###**
*####
javainterviewpoint says
Here you go
Shibananda says
Java is very interesting
Aayushrathi says
It was very helpful. Please give a program for this too.
*
#*
*#*
#*#*
*#*#*
javainterviewpoint says
Here you go
tushar gupta says
**********
**** ****
*** ***
** **
* *
Make this methoud java program please
javainterviewpoint says
Here you go
xristos says
can u make this pattern in java?
given n=3 for example:
**1
*23
456
or given n=4
***1
**23
*456
78910
javainterviewpoint says
Here you go, apologies for the delayed response