You should know that different pattern programs can be developed using different programming languages. And Java programming language is no different from that. We can also develop โPatterns in Javaโ using the for loops & conditional statements.
Pattern programs are the most important sector for questions in every interview to enter into the software development field. In this article, we will discuss 30+ pattern programs in Java programming language.
So, let us start our discussion.
Summary or Key Highlights:ย
- Pattern program is a popular practice where characters & numbers are placed in a certain order.
- Based on the elements used for the printing of patterns, the categories are defined.
- The use of Conditional Statements & loops are must to work on pattern programs.
- Think logically from the Row & Column viewpoint to understand the concept.
- You have to execute nested loops to get certain pattern programs.
How Many Design Patterns In Java?
In every programming language, pattern programs can be implemented using different kinds of elements. Based on those elements, the pattern programs can be differentiated into three parts. In the case of Java, the same thing is applied.
The patterns in Java programming language can be divided into three categories:
- Java Character Patterns
- Java Special Character Patterns or Java Star Patterns
- Java Numeric Patterns
We will learn about each of them one by one along with sample examples & their output.
How To Print Character Patterns in Java In Different Ways?
In such pattern programs, alphabetic characters are used to develop the design. Sometimes, the alphabetic character increases or decreases in order along with the number of rows value.
We can classify it into Ten parts.
1. Triangle Character Pattern Program:
public class Main
{
public static void main(String[] args) {
int a = 115; // ASCII Value Of Character
for (int i = 0; i <= 2; i++) // Outer Loop
{
for (int j = 0; j <= i; j++) // Inner Loop
{
System.out.print((char) (a + j) + " "); // Print Pattern
}
System.out.println();
}
}
}
Output:
2. Row-Wise Alphabetic Triangle Pattern:
public class Main
{
public static void main(String[] args) {
int a = 115; // ASCII Value Of Character
for (int i = 0; i<= 2; i++) // Outer Loop
{
for (int j = 0; j <= i; j++) // Inner Loop
{
System.out.print((char) a + " "); // Print Pattern
}
a++;
System.out.println();
}
}
}
Output:
3. K-Style Alphabetic Character Pattern:
public class Main
{
/* Program Starts With Public Static Void Main (String[] args)Function*/
public static void main(String[] args) {
for (int i = 2; i >= 0; i--) // Outer Loop For Upper Part
{
int a = 115; // ASCII Value Of Upper Character
for (int j = 0; j <= i; j++) // Inner Loop For Upper Part
System.out.print((char) (a + j) + " "); // Printing Value
System.out.println();
}
for (int i = 0; i<= 2; i++) // Outer Loop For Lower Part
{
int b = 75; // ASCII Value Of Lower Character
for (int j = 0; j <= i; j++) // Inner Loop For Lower Part
{
System.out.print((char) (b + j) + " "); // Print Pattern
}
System.out.println();
}
}
}
Output:
4. Increasing Character In Triangle Pattern:
public class Main
{
public static void main(String[] args) {
for (int i = 0; i <= 2; i++) { // Outer Loop
int a = 115; // ASCII Value Of Character
for (int j = 2; j > i; j--) // Inner Loop
System.out.print(" ");
for (int k = 0; k <= i; k++) // Inner Loop
System.out.print((char) (a + k) + " "); // Print Pattern
System.out.println();
}
}
}
Output:
5. Diamond Shape Character Pattern:
public class Main {
public static void main(String[] args) {
int s = 3;
for (int i = 0; i < s; i++) { // For Loop For Printing the Upper Pyramid
for (int j = s - i - 1; j > 0; j--) { // Inner Loop For Spaces
System.out.print(" ");
}
for (int k = 0; k <= i; k++) { // For Loop For Printing Alphabets In Ascending
System.out.print((char)('C' + k));
}
for (int l = i - 1; l >= 0; l--) { // For Loop For Printing Alphabets In Descending
System.out.print((char)('C' + l));
}
System.out.println();
}
for (int i = s - 2; i >= 0; i--) { // For Loop For Printing the Down Pyramid
for (int j = s - i - 1; j > 0; j--) { // Inner Loop For Spaces
System.out.print(" ");
}
for (int k = 0; k <= i; k++) { // For Loop For Printing Alphabets In Ascending
System.out.print((char)('C' + k));
}
for (int l = i - 1; l >= 0; l--) { // For Loop For Printing Alphabets In Descending
System.out.print((char)('C' + l));
}
System.out.println();
}
}
}
Output:
6. Upper & Lower Character Pattern:
public class Main {
public static void main(String[] args) {
int n = 5; // Number Of Rows In Pattern
char cc = 'C'; // Starting Character In Of Pattern
for (int i = 1; i <= n; i++) { // Outer Loop For Column
for (int j = 1; j <= i; j++) { // Inner Loop For Row
if (j % 2 == 1) { // Marking The Posstion
System.out.print(Character.toLowerCase(cc) + " "); // Making Lowercase
} else {
System.out.print(cc + " "); // Printing Current Letter
}
cc++;
}
System.out.println();
}
}
}
Output:
7. Reverse Triangle Pattern:
public class Main {
public static void main(String[] args) {
int s = 5; // Size Of The Pattern
for (int i = 0; i < s; i++) { // Outer Loop For Column
for (int j = s - 1; j >= i; j--) { // Inner Loop For Printing Alphabets
System.out.print((char) ('C' + j) + " ");
}
System.out.println();
}
}
}
Output:
8. Stair Character Pattern:
public class Main {
public static void main(String[] args) {
int s = 3; // Size Of The Pattern
for (int i = 0; i < s; i++) { // Outer Loop For Column
for (int j = s - 1; j > i; j--) { // Inner Loop For Printing Spaces
System.out.print(" ");
}
for (int k = 0; k <= i; k++) { // Inner Loop For Printing Alphabets
System.out.print((char) ('C' + i) + " ");
}
System.out.println();
}
}
}
Output:
9. Pyramid Character Pattern:
class Main {
public static void main(String[] args) {
int s = 5; // Size Of The Pattern
char cc = 'C'; // Starting Character Of The Pattern
for (int i = 1; i <= s; i++) { // Outer Loop For Column
for (int j = 1; j <= s - i; j++) { // Inner Loop For Printing Spaces
System.out.print(" ");
}
for (int k = 1; k <= i; k++) { // Inner Loop For Printing Alphabets
System.out.print(cc + " ");
cc++;
}
System.out.println();
}
}
}
Output:
10. Rectangle Character Pattern:
public class Main {
public static void main(String[] args) {
int r = 3; // Rows In The Pattern
int c = 5; // Columns In The Pattern
char sc = 'C'; // Starting Character Of Pattern
for (int i = 0; i < r; i++) { // Outer Loop For Rows
for (int j = 0; j < c; j++) { // Inner Loop For Printing Alphabets
char cc = (char) (sc + j); // Getting Alphabets
System.out.print(cc + " "); // Printing Letters
}
System.out.println();
}
}
}
Output:
How Many Pattern Programs Belong To Java Special Character Or Star Patterns?
In such pattern programs, special characters like *,#,@,& are used to develop the design. In this case, we have implemented star pattern programs or special character pattern programs using the character โ@โ.
Using some loops & conditions such designs are built along with the help of the number of rows. We can classify it into eleven parts. If you face any difficulties learning about the below pattern programs then worry not, you canย get Java HW help from CodingZap experts and clear your confusion.
1. Hollow Diamond Shape Pattern Program:
public class Main
{
public static void main(String[] args) { // Diamond Star Pattern Enter
int r = 3; // Number Of Rows
for (int i=1 ; i<= r ; i++) // Outer Loop
{
for (int j = r; j > i ; j--)
System.out.print(" ");
System.out.print("@");
for (int k = 1; k < 2*(i -1) ;k++)
System.out.print(" ");
if(i==1)
System.out.println("");
else
System.out.println("@"); // Print Pattern
}
for (int i=r-1; i>= 1 ; i--)
{
for (int j = r; j > i ; j--)
System.out.print(" ");
System.out.print("@"); // Print Pattern
for (int k = 1; k < 2*(i -1) ;k++)
System.out.print(" ");
if(i==1)
System.out.println("");
else
System.out.println("@"); // Print Pattern
}
}
}
Output:
2. Downward Special Character Triangle:
public class Main
{
public static void main(String[] args) { // Down Triangle Star Pattern Enter
int r = 3; // Number Of Rows
for (int i=r; i>= 1 ; i--) // Outer Loop
{
for (int j = i; j < r ; j++)
System.out.print(" ");
for (int k = 1; k <= (2*i -1) ;k++) {
if(k==1 || i == r || k == (2*i-1))
System.out.print("@"); // Print Pattern
else
System.out.print(" ");
}
System.out.println("");
}
}
}
Output:
3. Upward Special Character Triangle:
public class Main
{
/* Program Starts With Public Static Void Main (String[] args)Function*/
public static void main(String[] args) {
int r = 3; // Number of Rows
for (int i=1; i<= r ; i++) // Outer Loop
{
for (int j = i; j < r ; j++)
System.out.print(" ");
for (int k = 1; k <= (2*i -1) ;k++) {
if( k==1 || i == r || k==(2*i-1))
System.out.print("@"); // Print Pattern
else
System.out.print(" ");
}
System.out.println("");
}
}
}
/* Here, the program also resembles with Pyramid star pattern or special character pattern */
Output:
4. Left Pascalโs Triangle Program With Special Characters:
public class Main
{
/* Program Starts With Public Static Void Main (String[] args)Function*/
public static void main(String[] args) {
int r = 3; // Number Of Rows
for (int i= 1; i<= r ; i++) // Outer Loop
{
for (int j=i; j < r ;j++)
System.out.print(" ");
for (int k=1; k <= i ; k++)
System.out.print("@"); // Left Triangle Pascal's Enter
System.out.println("");
}
for (int i=r; i>=1; i--)
{
for(int j=i; j<=r;j++)
System.out.print(" ");
for(int k=1; k
Output:
5. Right Pascalโs Triangle With Special Character:
public class Main
{
/* Program Starts With Public Static Void Main (String[] args)Function*/
public static void main(String[] args) {
int r = 3; // Number Of Rows
for (int i= 0; i<= r-1 ; i++) // Outer Loop
{
for (int j=0; j<=i; j++)
{
System.out.print("@"); // Pattern Programs Enter
System.out.print(" ");
}
System.out.println("");
}
for (int i=r-1; i>=0; i--)
{
for(int j=0; j <= i-1;j++)
{
System.out.print("@"); // Print Character
System.out.print(" ");
}
System.out.println("");
}
}
}
Output:
6. Reverse Pyramid Program With Special Character:
public class Main
{
/* Program Starts With Public Static Void Main (String[] args)Function*/
public static void main(String[] args) {
int r = 3; // Number Of Rows Value
for (int i= 0; i<= r-1 ; i++) // Outer Loop
{
for (int j=0; j<=i; j++)
System.out.print(" ");
for (int k=0; k<=r-1-i; k++)
{
System.out.print("@"); // Printing Pattern
System.out.print(" ");
}
System.out.println();
}
}}
Output:
7. Right Triangle Star Pattern Or Special Character Pattern:
public class Main
{
// Triangle Star Program Enter means starting the field
public static void main(String[] args) {
int i;
int j;
int n = 3; // Number Of Rows
for(i=0; i
Output:
8. Reverse Right Triangle Special Character Pattern:
public class Main
{
/* Mirror Star Pattern Enter or Starting of Reverse Right Triangle Special Character Pattern */
public static void main(String[] args) {
int i;
int j;
int r = 3; // Number Of Rows
for (i = r-1; i>=0 ; i--) // Outer Loop
{
for (j = 0; j<=i; j++) // Inner Loop
System.out.print("@ ");
System.out.println();
}}}
Output:
9. Left Triangle Star Pattern Or Special Character Pattern (Mirror Of Right Angled):
public class Main
{
public static void main(String[] args) {
int r = 2; // Number Of rows
for (int i= 0; i<= r; i++) // Outer Loop
{
for (int j=1; j<=r-i; j++) // Inner Loop
System.out.print(" ");
for (int k=0;k<=i;k++) // Inner Loop
System.out.print("@"); // Printing Pattern
System.out.println();
}
}}
Output:
10. Reverse Left Triangle Special Character Pattern:
public class Main
{
public static void main(String[] args) {
int r = 3; // Number Of Rows
for (int i = r; i>= 1; i--) // Outer Loop
{
for (int j = r; j>i; j--) // Inner Loop
System.out.print(" ");
for (int k=1;k<=i;k++) // Inner Loop
System.out.print("@"); // Printing Pattern
System.out.println();
}
}}
Output:
#11 Full Diamond Shape Pattern Program:
public class Main
{
public static void main(String[] args) {
int r = 2; // Number Of Rows
int i;
int j;
int a = r-1; // Number Of Space
for (j = 1; j<= r; j++) // Outer Loop For Upper Part
{
for (i = 1; i<= a; i++) // Inner Loop For Upper Part
System.out.print(" ");
a--;
for (i = 1; i <= 2 * j - 1; i++) // Inner Loop For Upper Part
System.out.print("@");
System.out.println();
}
a = 1;
for (j = 1; j<= r - 1; j++) // Outer Loop For Lower Part
{
for (i = 1; i<= a; i++) // Inner Loop For Lower Part
System.out.print(" ");
a++;
for (i = 1; i<= 2 * (r - j) - 1; i++) // Inner Loop For Lower Part
System.out.print("@");
System.out.println();
}
}}
Output:
Conclusion:
From the above discussion, we hope, the concept of โPatterns in Javaโ is much more clear to you.
All the sample programs are implemented by taking the Public Static Void Main function as the base. We have not declared any special user-defined function besides the Public Static Void Main function. You should remember that.
We advise you to focus on the basic concept of Java programming language, more specifically on loops & conditional statements. Because the pattern programs are the result of particular usage of such concepts. Loops are an interesting concept and loops have different features in each programming language. Like,ย a loop in Python is used for iteration. If you get the condition for implementing that, it will become a cakewalk for you. If you want to know more about patterns in Java, then you can read our article on MVC architecture in Javaย and know about patterns.
Takeaways:ย
- To draw patterns, we can use Characters, Stars, or Special Characters & Numbers in Java.
- Based upon the pattern, we can provide different names like Pyramid, Diamond, etc.
- You have to think from the Row & Column perspective to get the correct logic.
- The inner & outer loops will represent the Row & Column respectively.
- The use of Conditional If-Else will help to place the elements in the correct order.