Java Interviews can give a hard time for programmers, such as the severity of the process. The ones who have attended the process will know that a pattern program ought to pop up in the list of programs. This article precisely focuses on pattern programs in Java. I have classified the programs under the following clusters:
A pattern that repeats a certain number of times is known as a pattern. In reality, its pattern might be decorative, like the example of a rangoli. An algorithm and looping idea improvement program written in Java. Nesting loops make it simple to show them. While both the while and for loops may be used to create loops, the for loop is easier to write into programs than the while loop is. Rows are displayed using the outer loop, while columns are displayed using the inner loop (assume Matrix). Because of this, the inner loop shows the values in those columns while the outer loop regulates the number of rows.
Star Program Algorithm:
- Print the star pattern from the end of each Row
- Complete the last column of each Row
- Start from the Second Last Column of the second row
- Repeat till the number of rows is specified by the User.
Every pattern program contains two or more loops. The depth of the pattern or logic determines how many loops there are. Both the first and second for loops are effective for the row and column, respectively. Java for loops is frequently used in pattern applications.
In the above pattern, i stands for the row and j for the column. The first row merely prints a star, as can be seen. Two stars are printed in the second row, and so on. The gaps are printed using colored blocks.
Star program in Java for Pyramid pattern:
*
* *
* * *
* * * *
* * * * *
The code to create the pyramid star pattern is given below:
class Star { public static void printTriagle(int n) { for (int i = 0; i < n; i++) { for (int j = n - i; j > 1; j--) { //Loop for blank space System.out.print(" "); //Print Space } for (int j = 0; j <= i; j++) { System.out.print("* "); //Print Star } System.out.println(); //Newline } } public static void main(String args[]) { int n = 5; printTriagle(n); } }
Star program in Java for Right Triangle Star Pattern:
*
* *
* * *
* * * *
* * * * *
* * * * * *
The code for the right triangle star pattern is given below:
class RightTrianglePattern { public static void main(String args[]) { int i, j, row=6; for(i=0; i<row; i++) { for(j=0; j<=i; j++) { System.out.print("* "); } System.out.println(); } } }
Star program in Java for Mirrored Right Triangle Star Pattern
*
* *
* * *
* * * *
* * * * *
* * * * * *
The code for the mirrored right triangle star pattern is given below:
class LeftTrianglePattern { public static void main(String args[]) { int i, j, row = 6; for (i=0; i<row; i++) { for (j=2*(row-i); j>=0; j--) { System.out.print(" "); } for (j=0; j<=i; j++ ) { System.out.print("* "); } System.out.println(); } } }
Star program in Java for Diamond Shape Star Pattern
*
***
*****
*******
*********
*******
*****
***
*
The code for the diamond shape star pattern is given below:
import java.util.Scanner; class DiamondPattern { public static void main(String args[]) { int row, i, j, space = 1; System.out.println("Enter the number of rows you want to print: "); Scanner sc = new Scanner(System.in); row = sc.nextInt(); space = row - 1; for (j = 1; j<= row; j++) { for (i = 1; i<= space; i++) { System.out.print(" "); } space--; for (i = 1; i <= 2 * j - 1; i++) { System.out.print("*"); } System.out.println(""); } space = 1; for (j = 1; j<= row - 1; j++) { for (i = 1; i<= space; i++) { System.out.print(" "); } space++; for (i = 1; i<= 2 * (row - j) - 1; i++) { System.out.print("*"); } System.out.println(""); } } }
Star program in Java for Inverted Pyramid Star Pattern:
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
The code for the inverted pyramid is given below:
class ReversePyramidPattern { public static void main(String[] args) { int rows=8; for (int i= 0; i<= rows-1; i++) { for (int j=0; j<=i; j++) { System.out.print(" "); } for (int k=0; k<=rows-1-i; k++) { System.out.print("*" + " "); } System.out.println(); } } }
Conclusion:
So far, we’ve seen some star programs in Java for printing different Star patterns. Anyone who is in the initial phase of coding, must implement star problems for improving and increasing their efficiency. Includes formulas to help you learn and comprehend things step by step.
Other Java Programs
Java Program to Add Two Numbers
Java Program to Check Prime Number
Java Program to Check Whether a Number is a Palindrome or Not
Java Program to Find the Factorial of a Number
Java Program to Reverse a Number
Java Program to search an element in a Linked List
Program to convert ArrayList to LinkedList in Java
Java Program to Reverse a linked list
Java Program to search an element in a Linked List
Anagram Program in Java
Inheritance Program in Java
Even Odd Program in Java
Hello World Program in Java
If else Program in Java
Binary Search Program in Java
Linear Search Program in Java
Menu Driven Program in Java
Package Program in Java
Leap Year Program in Java
Array Programs in Java
Linked List Program in Java
String Programs in Java