30+ Patterns in JAVA

30+ Patterns in JAVA

Hello I’m Akshay Bendadi and currently pursuing the BE computer engineering in 2nd year.

Today In this blog, we will be implementing a program that generates various star patterns using the Java programming language. Star patterns are a common way to practice looping and conditional statements in programming, and they can also be used to create simple graphics or decorative elements in programs.

The goal of this project is to develop a program that can generate a variety of star patterns using nested loops and conditional statements. We will be implementing several different patterns, including pyramid patterns, diamond patterns, and hourglass patterns, among others.

To demonstrate the use of these patterns, we will be creating a simple user interface that allows users to select the type of pattern they want to generate and the size of the pattern. This will provide an opportunity to see how these patterns can be customized and used in a real-world scenario.

Overall, this project will provide a deeper understanding of how looping and conditional statements work in Java and how they can be used to create simple graphics and decorative elements.

Pattern 1

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern1(n);
}
static void pattern1(int n)
{
System.out.println("pattern 1:-");
for (int row = 1; row <= n; row++)
{
for (int col = 1; col <= n; col++)
{
System.out.print("* ");
}
System.out.println();
}
}
}

Pattern 2

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern2(n);
}
static void pattern2(int n)
{
System.out.println("pattern 2:-");
for (int row = 1; row <=n; row++)
{
for (int col = 1; col <= row; col++)
{
System.out.print("* ");
}
System.out.println();
}
}
}

Pattern 3

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern3(n);
}
static void pattern3(int n)
{
System.out.println("pattern 3:-");
for (int row = n; row >= 1; row--)
{
for (int col = 1; col <= row; col++)
{
System.out.print("* ");
}
System.out.println();
}
}
}

Pattern 4

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern4(n);
}
static void pattern4(int n)
{
System.out.println("pattern 4:-");
for (int row = 1; row <=n; row++)
{
for (int col = 1; col <= row; col++)
{
System.out.print(col +" ");
}
System.out.println();
}
}
}

Pattern 5

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern5(n);
}
static void pattern5(int n)
{
System.out.println("pattern 5:-");
for(int row = 1 ; row < 2 * n ; row++)
{
int totalcolumntoprint = row > n ? 2 * n - row : row;
for (int col = 1; col <= totalcolumntoprint ; col++)
{
System.out.print("* ");
}
System.out.println();
}
}
}

Pattern 6

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern6(n);
}
static void pattern6(int n)
{
System.out.println("pattern 6:-");
for(int row = 1 ; row <= n ; row++)
{
int noofspaces = n - row;
for(int space = 1 ; space <= noofspaces ; space++)
{
System.out.print(" ");
}
for (int col = 1; col <= row; col++)
{
System.out.print("*");
}
System.out.println();
}
}
}

Pattern 7

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern7(n);
}
static void pattern7(int n)
{
System.out.println("pattern 7:-");
for(int row = n ; row >= 1 ; row--)
{
int noofspaces = n - row;
for(int space = 1 ; space <= noofspaces ; space++)
{
System.out.print(" ");
}
for (int col = 1; col <= row; col++)
{
System.out.print("*");
}
System.out.println();
}
}
}

Pattern 8

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern8(n);
}
static void pattern8(int n)
{
System.out.println("pattern 8:-");
for(int row = 1 ; row <= n ; row++)
{
int noofspaces = n- row;
for (int space = 1; space <=noofspaces ; space++) {
System.out.print(" ");
}
for (int col = row; col >= 1; col--) {
System.out.print("* ");
}
for (int l = 2; l <=row ; l++) {
System.out.print("* ");
}
System.out.println();
}
}
}

Pattern 9

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern9(n);
}
static void pattern9(int n)
{
System.out.println("pattern 9:-");
for(int row = n ; row >= 1 ; row--)
{
int noofspaces = n- row;
for (int space = 1; space <=noofspaces ; space++) {
System.out.print(" ");
}
for (int col = row; col >= 1; col--) {
System.out.print("* ");
}
for (int l = 2; l <=row ; l++) {
System.out.print("* ");
}
System.out.println();
}
}
}

Pattern 10

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern10(n);
}
static void pattern10(int n)
{
System.out.println("pattern 10:-");
for(int row = 1 ; row <= n ; row++)
{
int noofspaces = n - row;
for(int space = 1 ; space <= noofspaces ; space++)
{
System.out.print(" ");
}
for (int col = 1; col <= row; col++)
{
System.out.print("* ");
}
System.out.println();
}
}
}

Pattern 11

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern11(n);
}
static void pattern11(int n)
{
System.out.println("pattern 11:-");
for(int row = n ; row >= 1 ; row--)
{
int noofspaces = n - row;
for(int space = 1 ; space <= noofspaces ; space++)
{
System.out.print(" ");
}
for (int col = 1; col <= row; col++)
{
System.out.print("* ");
}
System. out.println();
}
}
}

Pattern 12

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern12(n);
}
static void pattern12(int n)
{
System.out.println("pattern 12:-");
for(int row = n * 2; row >= 1 ; row--)
{
int c = row > n ? row - n : n - row + 1 ;
for(int space = 1 ; space <= n - c ; space++)
{
System.out.print(" ");
}
for (int col = 1; col <= c; col++)
{
System.out.print("* ");
}
System.out.println();
}
}
}

Pattern 13

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern13(n);
}
static void pattern13(int n)
{
System.out.println("pattern 13:-");
for (int row = 1; row <= n; row++) {
int c = n - row;
for (int space = 0; space < c; space++) {
System.out.print(" ");
}
for (int col = row; col >= 1; col--)
{
if (col == row) {
System.out.print("*");
}
else if (row==n) {
System.out.print("*");
}
else {
System.out.print(" ");
}
}
for (int rcol = 2; rcol <=row ; rcol++)
{
if (rcol == row) {
System.out.print("*");
}
else if (row == n){
System.out.print("*");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}
}

Pattern 14

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern14(n);
}
static void pattern14(int n)
{
System.out.println("pattern 14:-");

for(int row = n ; row >= 1 ; row--)
{
int noofspaces = n- row;
for (int space = 1; space <=noofspaces ; space++)
{
System.out.print(" ");
}
for (int col = row; col >= 1; col--)
{
if (col == row) {
System.out.print("*");
}
else if (row==n) {
System.out.print("*");
}
else {
System.out.print(" ");
}
}
for (int l = 2; l <=row ; l++)
{
if (l == row) {
System.out.print("*");
}
else if (row == n){
System.out.print("*");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}
}

Pattern 15

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern15(n);
}
static void pattern15(int n)
{
System.out.println("pattern 15:-");
for (int row = 0; row < n; row++) {
for (int space = 0; space < n - row - 1; space++) {
System.out.print(" ");
}
System.out.print("*");
for (int col = 0; col < 2 * row - 1; col++) {
System.out.print(" ");
}
if (row > 0) {
System.out.print("*");
}
System.out.println();
}
for (int row = n - 2; row >= 0; row--) {
for (int space = 0; space < n - row - 1; space++) {
System.out.print(" ");
}
System.out.print("*");
for (int col = 0; col < 2 * row - 1; col++) {
System.out.print(" ");
}
if (row > 0) {
System.out.print("*");
}
System.out.println();
}
}
}

Pattern 16

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern16(n);
}
static void pattern16(int n)
{
System.out.println("pattern 16:-");
int num = 1;
for (int row = 0; row < n; row++)
{
for (int col = n-row; col > 1; col--)
{
System.out.print(" ");
}
for (int col = 0; col <= row; col++)
{
if (col == 0 || row == 0)
num = 1;
else
num = num*(row-col+1)/col;
System.out.print(num+" ");
}
System.out.println();
}
}
}

Pattern 17

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern17(n);
}
static void pattern17(int n)
{
System.out.println("pattern 17:-");
for (int row = 0; row < 2 * n; row++)
{
int c = row > n ? 2* n - row : row;
for (int space = 0; space < n - c; space++)
{
System.out.print(" ");
}
for (int col = c; col >= 1 ; col--)
{
System.out.print(col + " ");
}
for (int col = 2; col <= c; col++)
{
System.out.print(col +" ");
}
System.out.println();
}
}
}

Pattern 18

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern18(n);
}
static void pattern18(int n)
{
System.out.println("pattern 18:-");
for (int row = n * 2; row >= 1; row--)
{
int c = row > n ? row - n : n - row + 1;

for (int col = 1; col <= c ; col++)
{
System.out.print("*" + " ");
}
for (int space = 1; space <= n - c; space++)
{
System.out.print(" ");
}
for (int space = n-c; space >= 1; space--)
{
System.out.print(" ");
}
for (int col = c; col >= 1; col--)
{
System.out.print("*" +" ");
}
System.out.println();
}
}
}

Pattern 19

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern19(n);
}
static void pattern19(int n)
{
System.out.println("pattern 19:-");
for(int row = 1 ; row <= 2 * n ; row++)
{
int c= row > n ? 2 * n - row : row;
for (int j = 1; j <=c ; j++)
{
System.out.print("*");

}
for (int space = 1; space <=n-c; space++)
{
System.out.print(" ");
}
for (int col = c; col >=1 ; col--) {

System.out.print("*");
}
System.out.println();
}
}
}

Pattern 20

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern20(n);
}
static void pattern20(int n)
{
System.out.println("pattern 20:-");
for (int row=1; row<=5; row++)
{
for (int col=1; col<n; col++)
{
if (row==1 || row==n || col==1 || col == n-1)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}

Pattern 21

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern21(n);
}
static void pattern21(int n)
{
System.out.println("pattern 21:-");
int sum = 1;
for (int row = 1; row <= n; row++)
{
for (int col = 1; col <= row; col++)
{
System.out.print(sum + " ");
sum=sum+1;
}
System.out.println();
}
}
}

Pattern 22

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern22(n);
}
static void pattern22(int n)
{
System.out.println("pattern 22:-");
int sum = 0;
for (int row = 1; row <= n; row++)
{
if ( row % 2 == 0)
sum=0;
else
sum=1;
for (int col = 1; col <= row; col++)
{
if(sum == 1)
{
System.out.print(sum + " ");
sum = 0;
}
else
{
System.out.print(sum + " ");
sum = 1;
}
}
System.out.println();
}
}
}

Pattern 23

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern23(n);
}
static void pattern23(int n)
{
System.out.println("pattern 23:-");
for(int i = 1 ; i <= 2 * n ; i++)
{
int c= i > n ? 2 * n - i : i;
for (int j = 1; j <=c ; j++)
{
if (j==1 || j == c) {
System.out.print("*");
}
else {
System.out.print(" ");
}
}
for (int j = 1; j <=n-c; j++)
{
System.out.print(" ");
}
for (int j = c; j >=1 ; j--) {
if (j == 1 || j == c) {
System.out.print("*");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}
}

Pattern 24

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern24(n);
}
static void pattern24(int n)
{
System.out.println("pattern 24:-");
for(int i = 1;i <= n ; i++)
{
for(int k = 1; k<= n-i ; k++)
{
System.out.print(" ");
}
for(int j = 1; j <= n; j++)
{
if (i==1 || i==n || j==1 || j == n)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}

Pattern 25

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern25(n);
}
static void pattern25(int n)
{
System.out.println("pattern 25:-");
int sum = 0;
for (int i = n; i >= 1 ; i--)
{
sum += 1;
for (int j = 1; j <=i ; j++)
{
System.out.print(sum + " ");
}
System.out.println();
}
}
}

Pattern 26

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern26(n);
}
static void pattern26(int n)
{
System.out.println("pattern 26:-");
n = 4;
int count = 1;

// Iterate through each row
for (int i = 0; i < n; i++)
{
// Iterate through each column
for (int j = 0; j < n; j++)
{
// If row number is greater than
// column number
if (i > j) {
System.out.print(" ");
} else {
// Print the number
System.out.print(count + " ");
count++;
}
}

if (i % 2 == 0)
count = count + (n - 1);
else
count = count + (n + 1);

System.out.println();
}
}
}

Pattern 27

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern27(n);
}
static void pattern27(int n)
{
System.out.println("pattern 27:-");
for (int row = 1; row < 2 * n ; row++)
{
int c = row > n ? 2 * n - row : row;
for (int space = 1; space <= n - c ; space++)
{
System.out.print(" ");
}
for (int col = 1; col <= c; col++)
{
System.out.print("* ");
}
System.out.println();
}
}
}

Pattern 28

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern28(n);
}
static void pattern28(int n)
{
System.out.println("pattern 28:-");
for (int row = 1; row < 2 * n; row++)
{
int c = row > n ? 2 * n - row : row;
for (int col = 1; col <= c; col++)
{
System.out.print("*");
}
for (int space = 1; space <= n - c ; space++)
{
System.out.print(" ");
};
for (int col = c; col >= 1; col--)
{
System.out.print("*");
}
System.out.println();
}
}
}

Pattern 29

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern29(n);
}
static void pattern29(int n)
{
System.out.println("pattern 29:-");
for (int row = 1; row <= n; row++)
{
int c = n - row;
for (int space = 1; space <=c ; space++)
{
System.out.print(" ");
}
for (int col = row; col >= 1; col--)
{
System.out.print(col + " ");
}
for (int col = 2; col <= row; col++)
{
System.out.print(col + " ");
}
System.out.println();
}
}
}

Pattern 30

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern30(n);
}
static void pattern30(int n)
{
System.out.println("pattern 30:-");
int originalN = n;
n = n*2-1;
for (int row = 1; row <= n; row++)
{
for (int col = 1; col <= n; col++)
{
int c = originalN - Math.min(Math.min(row-1,col-1),Math.min(n-row,n-col));
System.out.print(c);
}
System.out.println();
}
}
}

Pattern 31

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern31(n);
}
static void pattern31(int n)
{
System.out.println("pattern 31:-");
for(int row=n; row>=1; row--)
{
for(int col=row; col<=n; col++)
{
System.out.print((char)(col+64)+" ");
}
System.out.println();
}
}
}

Pattern 32

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern32(n);
}
static void pattern32(int n)
{
System.out.println("pattern 32:-");
int count = 1;
char ch = 'a';
for(int row=1; row<=n; row++)
{
for(int col=1; col<=row; col++)
{
if(count%2!=0) {
System.out.print((char) (ch++) + " ");
}
else {
System.out.print((char) (ch++ - 32) + " ");
}
count++;
}
System.out.println();
}
}
}

Pattern 33

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern33(n);
}
static void pattern33(int n)
{
System.out.println("pattern 33:-");
for(int row=n; row>=1; row--)
{
for(int col=1; col<=row; col++)
{
System.out.print((char)(row + 65 - col)+" ");
}
System.out.println();
}
}
}

Pattern 34

public class Pattern {
public static void main(String[] args)
{
int n = 5;
pattern34(n);
}
static void pattern34(int n)
{
System.out.println("pattern 34:-");
for (int row = 1; row <=n ; row++)
{
for (int col = 1; col <=row ; col++)
{
System.out.print(col+" ");
}
for (int space = 1; space <= 2 * (n - row) ; space++)
{
System.out.print(" ");
}
for (int col = row; col >=1 ; col--)
{
System.out.print(col+" ");
}
System.out.println();
}
}
}

I hope you’ve found this overview helpful and that you’ll consider implementing some of these patterns in your own projects. Remember, patterns are not one-size-fits-all solutions, so be sure to choose the right pattern for the right problem.

As always, thank you for reading. I hope you’ll continue to follow our blog for more programming tips and tricks. Happy coding!