pattern program in java

Pattern programs in Java

Pattern programs in java are the programs that have a series of numbers, alphabets or symbols in a particular fashion. We use loops to form pattern programs in java. Pattern class in java contains flags which are basically integer constants which can help us match the specific patterns. strategy makers, builders, observers etc. use design patterns. Some easy steps to create patterns in java are:

  • First of all, the coder analyses the symmetry of lines of the pattern. If you have to draw a symmetrical pattern, then there should be symmetry on both horizontal as well as vertical sides.
  • We first start by forming the upper part of the pattern towards the lower part.
  • The next step is to identify the element for each row and each column. i is the denotation of row set and j is the denotation for column set usually.
  • We then try to find the relation between the element and (i,j). The value of the total number will start with the number of elements and will keep decreasing as the pattern forms.

Pyramid Pattern of Stars in Java

import java.io.*; 
public class DeveloperHelps 
{ 
public static void printSymbols(int s) 
{ 
int i, j;
for(i=0; i<s; i++) { 
for(j=0; j<=i; j++){ 
System.out.print("* "); 
} 
System.out.println(); 
} 
} 
public static void main(String args[]) { 
int s = 7; 
printSymbols(s); 
} 
}


The output of the above code will be:

* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * * 
* * * * * * * 

Pyramid Pattern of Numbers

In the above program, we have passed an argument to print 7 symbols (s=7). The β€˜for’ loop helps in executing this pattern program in java and well as in other OOPs languages. We can print the number pattern in this form as well. In that case, the β€˜for’ loop will work like this:

import java.io.*; 
public class DeveloperHelps
{ 
public static void printNumbers(int s) 
{ 
int i, j,num; 
for(i=0; i<s; i++) 
{ 
num=1; 
for(j=0; j<=i; j++) 
{ 
System.out.print(num+ " ");  
num++; 
}    
System.out.println(); 
} 
}  
public static void main(String args[]) 
{ 
int s = 7; 
printNumbers(s); 
} 
}


The output of this program will be:

1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 5 6 
1 2 3 4 5 6 7

Odd number pattern program in Java

In the below program, only odd number pattern will be print in the form of Triangle.

import java.io.*; 
public class Developerhelps
{ 
public static void printNumbers(int s) 
{ 
int i, j,num; 
for(i=0; i<s; i++) 
{ 
num=1; 
for(j=0; j<=i; j++) 
{ 
System.out.print(num+ " ");  
num=num+2; 
}    
System.out.println(); 
} 
}  
public static void main(String args[]) 
{ 
int s = 11; // Number of lines
printNumbers(s); 
} 
}


Output :

1 
1 3 
1 3 5 
1 3 5 7 
1 3 5 7 9 
1 3 5 7 9 11 
1 3 5 7 9 11 13

There are specific problems for which we need specific design patterns. Say, if we have a problem where we want to create a class for which only a single object has to be created which can be used for multiple classes.

For this problem, we will use singleton pattern because it solves the above problem better than other pattern designs such as reduction pattern.

Character Pattern Programs in Java

An alphabet pattern program in java is a series of alphabets that generate a certain pattern or geometrical shapes like a square or triangle.

These patterns enhance your programming skills and are generated with the help of loops and if-else statements.

Left Triangle Alphabet Pattern

The left triangle alphabet pattern has a shape of a triangle with perpendicular on the left side made up of alphabets.

public class leftTrianlge {
  public static void main(String[] args) 
{
    int size = 5;
    int alpha = 65;
    for (int i = 0; i < size; i++)
    {
      for (int j = 0; j <= i; j++) 
      {
        System.out.print((char)(alpha+j));
      }
      System.out.println();
    }
  }
}


Output :

A
AB
ABC
ABCD
ABCDE

Right Triangle Alphabet Pattern

The right triangle alphabet pattern is an alphabet pattern that has a shape of a triangle with perpendicular to the right side.

public class rightTrianlge {
  public static void main(String[] args) 
{
    int size = 5;
    int alpha = 65;

    for (int i = 0; i < size; i++)
 {
      for (int j = 1; j < size - i; j++) 
     {
        System.out.print(" ");
      }
      for (int k = 0; k <= i; k++)
     {
        System.out.print((char)(alpha+k));
      }
      System.out.println();
    }
  }
}


Output :

    A
   AB
  ABC
 ABCD
ABCDE

Pattern Programs Uses

  • While designing something, a design pattern can be a solution to the repeatedly occurring problem.
  • Pattern design is also called as forever unfinished design. We can simply modify it and then transform it directly into the code.
  • It is a simple solution in the form of a template on how to solve a query that can be used in multiple situations.
  • Object-oriented developers perform these practices to make software development easy.
  • They are reusable and used in multiple projects.
  • With the help of pattern designs, we can maintain good structures.
  • They are transparent solutions for the application design.

One comment on “Pattern programs in Java

Leave a comment

Your email address will not be published. Required fields are marked *

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Java Online Quiz

Level up your coding skills with our interactive programming quiz!