Top 15 Pattern programs are a common requirement in Java interview processes, making them difficult for many programmers. Here we will discuss the top 15 Pattern Programs in Java: How to Print Star, Number, and Character. You can read Java from JavaGoal.com
Pattern Programs in Java
Star Patterns in Java
1. Pyramid Program in Java
// Import the Scanner class to read user input import java.util.Scanner; public class Pyramid { public static void main(String[] args) { // Create a Scanner object to read user input Scanner input = new Scanner(System.in); System.out.print("Enter the number of rows for the pyramid: "); // Read the user input for the number of rows int rows = input.nextInt(); // Loop through each row for (int i = 1; i <= rows; i++) { // Loop through each column to print spaces for (int j = 1; j <= rows - i; j++) { // Print a space System.out.print(" "); } // Loop through each column to print asterisks for (int k = 1; k <= 2 * i - 1; k++) { // Print an asterisk System.out.print("*"); } // Move to the next line after printing each row System.out.println(); } } }
Output:
Pyramid program in Java with explanation
Here is the explanation of the pyramid program in Java with user input, Let’s discuss it line-by-line:
// Import the Scanner class to read user input import java.util.Scanner;
The imports the Scanner class from Java.util package, which allows the program to read input from the user.
public class Pyramid {
It declares a public class called “Pyramid”.
public static void main(String[] args) {
Declares the main method of the program, which is the starting point of the program’s execution.
Scanner input = new Scanner(System.in);
Creates a new Scanner object called “input”, which is used to read input from the user.
System.out.print("Enter the number of rows for the pyramid: ");
It prints a message to the console asking the user to enter the number of rows for the pyramid.
int rows = input.nextInt();
Reads an integer input from the user using the Scanner object, and assigns the value to the integer variable “rows”.
for (int i = 1; i <= rows; i++) {
Starts a for loop that will iterate through each row of the pyramid, with the loop variable “i” starting at 1 and incrementing up to the number of rows specified by the user.
for (int j = 1; j <= rows - i; j++) {
It starts a nested for loop that will iterate through each column to print spaces before printing the asterisks for each row of the pyramid. The loop variable “j” starts at 1 and increments up to the number of spaces needed, which is equal to the difference between the number of rows and the current row number.
System.out.print(" ");
This line prints a space to the console.
for (int k = 1; k <= 2 * i - 1; k++) {
This line starts another nested for loop that will iterate through each column to print asterisks for each row of the pyramid. The loop variable “k” starts at 1 and increments up to the number of asterisks needed, which is equal to twice the current row number minus one.
System.out.print("*");
The main line prints an asterisk to the console.
System.out.println();
It moves the console cursor to the next line, which is necessary to start printing the next row of the pyramid.
}
The line ends the innermost for loop, which has been printing the asterisks for the current row of the pyramid.
}
It ends the outer for loop, which has been iterating through each row of the pyramid.
}
Ends the main method of the program.
Overall, this program prompts the user for input, creates a pyramid of asterisks with the specified number of rows, and prints it to the console. It uses a nested for-loop structure to print spaces and asterisks for each row of the pyramid, with the number of spaces and asterisks printed determined by the current row number.
2. Right Triangle Star Pattern in Java
import java.util.Scanner; /** * This program prompts the user to enter the number of rows for a Right triangle * made of stars, reads the user input using a Scanner object, and then prints * the triangle to the console using nested for loops. */ public class RightTriangleStarPattern { public static void main(String[] args) { // Create a Scanner object to read user input Scanner input = new Scanner(System.in); // Prompt the user to enter the number of rows System.out.print("Enter the number of rows for the Right triangle: "); // Read the user input and assign it to the variable "rows" int rows = input.nextInt(); // Outer loop to iterate through each row of the triangle for (int i = 1; i <= rows; i++) { // Inner loop to print the spaces before the stars for each row for (int j = 1; j <= (rows - i); j++) { System.out.print(" "); // Print a space to the console } // Inner loop to print the stars for each row for (int k = 1; k <= i; k++) { System.out.print("*"); // Print a star to the console } // Move the console cursor to the next line to start the next row System.out.println(); } } }
Output:
- In the first statement importing the Scanner class from Java.util package, that allow us to read user input from the console.
- In the main() method, a Scanner object is created to read user input.
- Firstly the program prompts the user to “enter the number of rows for the right triangle” by use of the print() method.
- The user input is read by use of the Scanner object and assigned to the variable “rows”.
- The outer loop is used to iterate through each row of the triangle. It starts with i=1 and continues until i is less than or equal to rows.
- The inner loop is used to print the spaces before the stars for each row. It starts with j=1 and continues until j is less than or equal to (rows – i).
- The System.out.print() method is used within the inner loop to print space to the console.
- The second inner loop is used to print the stars for each row. It starts with k=1 and continues until k is less than or equal to i.
- The System.out.print() method is used within the inner loop to print a star to the console.
- The System.out.println() method is used after the inner loops to move the console cursor to the next line to start the next row.
- Finally, the program ends with the closing of the main() method and the class definition.
3. Left Triangle Star Pattern in java
import java.util.Scanner; public class LeftTriangleStarPattern { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the number of rows for the pattern: "); int rows = sc.nextInt(); for (int i = 1; i <= rows; i++) { // loop through each row for (int j = 1; j <= i; j++) { // loop through each column in the current row System.out.print("* "); // print a star and a space } System.out.println(); // move to the next line } sc.close(); // close the scanner } }
Output:
4. Diamond Shape Pattern Program in Java
import java.util.Scanner; /** * This program prompts the user to enter the number of rows for a diamond * shape made of stars, reads the user input using a Scanner object, and then * prints the diamond to the console using nested for loops. */ public class DiamondShapePattern { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the number of rows for the diamond shape: "); int rows = input.nextInt(); int halfRows = (rows + 1) / 2; for (int i = 1; i <= halfRows; i++) { for (int j = 1; j <= (halfRows - i); j++) { System.out.print(" "); } for (int k = 1; k <= (2 * i - 1); k++) { System.out.print("*"); } System.out.println(); } for (int i = halfRows + 1; i <= rows; i++) { for (int j = 1; j <= (i - halfRows); j++) { System.out.print(" "); } for (int k = 1; k <= (2 * (rows - i) + 1); k++) { System.out.print("*"); } System.out.println(); } } }
Output:
Diamond Shape Pattern Program with explanation in Java
Explanation of diamond program with each line of this Java program:
import java.util.Scanner;
This line imports the Scanner class from java.util package. The Scanner class allows us to read input from the user.
public class DiamondShapePattern {
This line starts the definition of a new class called “DiamondShapePattern”. This class will contain a main method, which is the entry point for the program.
public static void main(String[] args) {
This line defines the main method of the program, which takes an array of String arguments as input.
Scanner input = new Scanner(System.in);
This line creates a new Scanner object called “input” that reads from the standard input stream (i.e., the keyboard).
System.out.print("Enter the number of rows for the diamond shape: ");
This line prints a message asking the user to enter the number of rows for the diamond shape.
int rows = input.nextInt();
This line reads an integer value entered by the user using the Scanner object, and stores it in a variable called “rows”.
int halfRows = (rows + 1) / 2;
This line calculates the number of rows needed for the top half of the diamond. If the user enters an even number of rows, this value will be rounded up to the nearest odd number.
for (int i = 1; i <= halfRows; i++) {
This line starts a for loop that will iterate over each row of the top half of the diamond. The loop variable “i” will take on values from 1 to “halfRows”.
for (int j = 1; j <= (halfRows - i); j++) { System.out.print(" "); }
This line starts a nested for loop that will print spaces before the first asterisk on each row. The loop variable “j” will take on values from 1 to “(halfRows – i)”.
for (int k = 1; k <= (2 * i - 1); k++) { System.out.print("*"); }
This line starts another nested for loop that will print asterisks for each row. The loop variable “k” will take on values from 1 to “(2 * i – 1)”.
System.out.println();
This line prints a newline character to move to the next row of the diamond.
for (int i = halfRows + 1; i <= rows; i++) {
This line starts a for loop that will iterate over each row of the bottom half of the diamond. The loop variable “i” will take on values from “(halfRows + 1)” to “rows”.
for (int j = 1; j <= (i - halfRows); j++) { System.out.print(" "); }
This line starts a nested for loop that will print spaces before the first asterisk on each row. The loop variable “j” will take on values from 1 to “(i – halfRows)”.
for (int k = 1; k <= (2 * (rows - i) + 1); k++) { System.out.print("*"); }
This line starts another nested for loop that will print asterisks for each row. The loop variable “k” will take on values from 1 to “(2 * (rows – i) + 1)”.
System.out.println();
This line prints a new line character to move to the next row of the diamond.
5. Downward Triangle Star Pattern in Java
import java.util.Scanner; /** * This program prompts the user to enter the number of rows for a downward * triangle star pattern, reads the user input using a Scanner object, and then * prints the triangle to the console using nested for loops. */ public class DownwardTriangleStarPattern { public static void main(String[] args) { // Create a new Scanner object to read user input Scanner input = new Scanner(System.in); // Ask the user to enter the number of rows for the triangle System.out.print("Enter the number of rows for the downward triangle: "); // Read the user input as an integer int rows = input.nextInt(); // Loop over each row of the triangle for (int i = rows; i >= 1; i--) { // Print spaces before the first asterisk on each row for (int j = 1; j <= rows - i; j++) { System.out.print(" "); } // Print asterisks for each row for (int k = 1; k <= 2 * i - 1; k++) { System.out.print("*"); } // Move to the next row of the triangle System.out.println(); } } }
Output:
Number Patterns in Java
6. Pyramid pattern with numbers in Java
import java.util.Scanner; public class PyramidProgram { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the number of rows for the pyramid: "); int rows = sc.nextInt(); int k = 0; // loop through each row for(int i = 1; i <= rows; ++i, k = 0) { // print spaces before the numbers in each row for(int j = 1; j <= rows - i; ++j) { System.out.print(" "); } // print the numbers in each row while(k != 2 * i - 1) { System.out.print(i + " "); ++k; } System.out.println(); // move to the next line } sc.close(); // close the scanner } }
Output:
7. Right triangle number pattern in Java
import java.util.Scanner; public class RightTriangleNumberPattern { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the number of rows for the pattern: "); int rows = sc.nextInt(); for (int i = 1; i <= rows; i++) { // loop through each row for (int j = 1; j <= (rows - i); j++) { // loop through each column before the numbers System.out.print(" "); // print two spaces } for (int k = 1; k <= i; k++) { // loop through each column for the numbers System.out.print(k + " "); // print the number and a space } System.out.println(); // move to the next line } sc.close(); // close the scanner } }
Output:
8. Left triangle number pattern in Java
import java.util.Scanner; public class LeftTriangleNumberPattern { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the number of rows for the pattern: "); int rows = sc.nextInt(); // loop through each row for (int i = 1; i <= rows; i++) { // loop through each column in the current row for (int j = 1; j <= rows; j++) { // print the current column number if it is less than or equal to the current row number if (j <= i) { System.out.print(j + " "); // print a space if the current column number is greater than the current row number } else { System.out.print(" "); } } // move to the next line System.out.println(); } sc.close(); // close the scanner } }
Output:
9. Diamond Shape number pattern in Java
import java.util.Scanner; public class DiamondShapeNumberPattern { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the number of rows for the pattern: "); int rows = sc.nextInt(); int space = rows - 1; // initialize the space variable // print upper half of diamond for (int i = 1; i <= rows; i++) { for (int j = 1; j <= space; j++) { // print spaces before the numbers System.out.print(" "); // print two spaces } space--; for (int j = 1; j <= 2 * i - 1; j++) { // print the numbers System.out.print(j + " "); } System.out.println(); // move to the next line } space = 1; // reset the space variable // print lower half of diamond for (int i = rows - 1; i >= 1; i--) { for (int j = 1; j <= space; j++) { // print spaces before the numbers System.out.print(" "); // print two spaces } space++; for (int j = 1; j <= 2 * i - 1; j++) { // print the numbers System.out.print(j + " "); } System.out.println(); // move to the next line } sc.close(); // close the scanner } }
Output:
10. Downward Triangle number pattern in Java
import java.util.Scanner; public class DownwardTriangleNumberPattern { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the number of rows for the pattern: "); int rows = sc.nextInt(); // print the pattern for (int i = rows; i >= 1; i--) { for (int j = 1; j <= i; j++) { // print the numbers System.out.print(j + " "); } System.out.println(); // move to the next line } sc.close(); // close the scanner } }
Output:
Character Patterns in Java
11. Pyramid Pattern Program with characters in Java
import java.util.Scanner; public class PyramidPattern { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the number of rows: "); int rows = sc.nextInt(); char ch = 'A'; for (int i = 1; i <= rows; ++i) { for (int space = 1; space <= rows - i; ++space) { System.out.print(" "); } for (int j = 0; j < 2 * i - 1; ++j) { System.out.print(ch + " "); if (j < i - 1) { ++ch; } else { --ch; } } System.out.println(); ch = 'A'; } } }
Output:
12. Right triangle pyramid program with character pattern in Java
import java.util.Scanner; public class RightTrianglePyramidPattern { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Get the number of rows from user input System.out.print("Enter the number of rows: "); int rows = sc.nextInt(); // Initialize a variable to keep track of the current character to print char currentChar = 'A'; // Loop through each row for (int i = 1; i <= rows; i++) { // Loop through each column in the current row for (int j = 1; j <= i; j++) { // Print the current character followed by a space System.out.print(currentChar + " "); // Increment the current character currentChar++; } // Move to the next line after printing all characters in the row System.out.println(); } } }
Output:
13. Left triangle pyramid program with character pattern in Java
import java.util.Scanner; public class RightTrianglePyramidPattern { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Get the number of rows from user input System.out.print("Enter the number of rows: "); int rows = sc.nextInt(); // Initialize a variable to keep track of the current character to print char currentChar = 'A'; // Loop through each row for (int i = 1; i <= rows; i++) { // Loop through each column in the current row for (int j = 1; j <= i; j++) { // Print the current character followed by a space System.out.print(currentChar + " "); // Increment the current character currentChar++; } // Move to the next line after printing all characters in the row System.out.println(); } } }
Output
Output:
14. Diamond shape program with character pattern in Java
import java.util.Scanner; public class DiamondPattern { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Get the number of rows from user input System.out.print("Enter the number of rows: "); int rows = sc.nextInt(); // Initialize a variable to keep track of the current character to print char currentChar = 'A'; // Print the upper half of the diamond for (int i = 1; i <= rows; i++) { // Print the required number of spaces before printing the characters in the row for (int j = rows - i; j > 0; j--) { System.out.print(" "); } // Loop through each column in the current row for (int j = 1; j <= 2 * i - 1; j++) { // Print the current character followed by a space System.out.print(currentChar + " "); // Increment the current character currentChar++; } // Move to the next line after printing all characters in the row System.out.println(); // Reset the current character back to 'A' after reaching the middle row if (i == rows / 2 + 1) { currentChar = 'A'; } } // Print the lower half of the diamond for (int i = rows - 1; i >= 1; i--) { // Print the required number of spaces before printing the characters in the row for (int j = rows - i; j > 0; j--) { System.out.print(" "); } // Loop through each column in the current row for (int j = 1; j <= 2 * i - 1; j++) { // Print the current character followed by a space System.out.print(currentChar + " "); // Increment the current character currentChar++; } // Move to the next line after printing all characters in the row System.out.println(); // Reset the current character back to 'A' after reaching the middle row if (i == rows / 2 + 1) { currentChar = 'A'; } } } }
15. Downward triangle character pattern in Java
import java.util.Scanner; public class DownwardTrianglePattern { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Get the number of rows from user input System.out.print("Enter the number of rows: "); int rows = sc.nextInt(); // Initialize a variable to keep track of the current character to print char currentChar = 'A'; // Print the downward triangle for (int i = rows; i >= 1; i--) { // Print the required number of spaces before printing the characters in the row for (int j = 1; j <= rows - i; j++) { System.out.print(" "); } // Loop through each column in the current row for (int j = 1; j <= i; j++) { // Print the current character followed by a space System.out.print(currentChar + " "); // Increment the current character currentChar++; } // Move to the next line after printing all characters in the row System.out.println(); } } }
Output: