Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
How to Print Pattern in Java?
3.
Star Patterns in Java
3.1.
1. ​​Pyramid Program
3.2.
Java
3.3.
2. Left Triangle Star Pattern
3.4.
Java
3.5.
3. Right Triangle Star Pattern
3.6.
Java
3.7.
4. Diamond Shape Pattern Program
3.8.
Java
3.9.
5. Downward Triangle Star Pattern
3.10.
Java
3.11.
6. Mirrored Right Triangle Star Pattern
3.12.
Java
3.13.
7. Reversed Pyramid Star Pattern
3.14.
Java
3.15.
8. Right down Mirror Star Pattern
3.16.
Java
3.17.
9. Right Pascal’s Triangle
3.18.
Java
3.19.
10. Left Triangle Pascal’s
3.20.
Java
3.21.
11. Sandglass Star Pattern
3.22.
Java
3.23.
12. Alphabet A Pattern
3.24.
Java
3.25.
13. Triangle Star Pattern
3.26.
Java
3.27.
14. Down Triangle
3.28.
Java
3.29.
15. Diamond Star Pattern
3.30.
Java
4.
Numeric Patterns in Java
4.1.
1. Simple Number Program
4.2.
Java
4.3.
2. Number Pattern Program in Java
4.4.
Java
4.5.
3. Diamond Pattern Program in Java
4.6.
Java
4.7.
4. Pascal’s Triangle Program in Java
4.8.
Java
4.9.
5. Number Pattern Program in Java
4.10.
Java
4.11.
6. Descending Order Pattern
4.12.
Java
4.13.
7. Binary Number Pattern
4.14.
Java
4.15.
8. Right Triangle Numeric Pattern
4.16.
Java
4.17.
9. Ones Pattern Program
4.18.
Java
4.19.
10. Diamond Numeric Pattern
4.20.
Java
5.
Character Patterns In Java
5.1.
Right Alphabetic Triangle
5.2.
Java
5.3.
Character Pattern Program
5.4.
Java
5.5.
K Shape Character Pattern
5.6.
Java
5.7.
Triangle Character Pattern
5.8.
Java
5.9.
Diamond Pattern
5.10.
Java
6.
Frequently Asked Questions
6.1.
Q. What is a pattern program in Java?
6.2.
Q. How many types of patterns are there in Java?
6.3.
Q. What are the top 3 design patterns in Java?
6.4.
Q. How to find patterns in Java?
7.
Conclusion
Last Updated: Apr 8, 2024
Easy

Pattern Programs in Java Language

Master Power BI using Netflix Data
Speaker
Ashwin Goyal
Product @
18 Jun, 2024 @ 01:30 PM

Introduction

Pattern programming in Java is the practice of creating various shapes or designs by combining symbols or characters. It involves utilizing loops and conditional statements to construct designs like triangles, squares, diamonds, and others.

In programming, pattern problems refer to challenges or exercises that require you to write code that generates a specific pattern of characters or symbols. 

Pattern problems in programming can be a fun and challenging way to develop your coding skills, as they require you to think creatively and use a combination of different programming concepts and techniques.  

Many people take pattern programs for granted, but it has a high weightage in Interviews to check your ability to think. 

Pattern Programs in Java

In this article, we will be discussing a bunch of different Pattern Programs in Java. So, let us start.

Also, check out Addition of Two Numbers in Java here.

How to Print Pattern in Java?

To print patterns in Java, you typically use nested loops. Define the number of rows and columns for the pattern and then use nested loops to iterate through each row and column, printing the desired characters or numbers according to the pattern. Adjust loop conditions and control structures to achieve various pattern designs such as triangles, squares, or other geometric shapes. Experiment with loop patterns and conditional statements to create intricate designs and structures.

The patterns can be classified as :

  1. Star Patterns
  2. Numeric Patterns
  3. Character Patterns
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Star Patterns in Java

Star problems pattern questions are a type of programming problem that involves printing out a pattern of stars in a specific shape or formation. 

These types of problems can be challenging, but they can also be a fun way to practice your programming skills.

1. ​​Pyramid Program

The Java code for printing a pyramid is given below:

  • Java

Java

import java.io.*;

public class Ninja
{
public static void print_pyramid(int n)
{
// outer loop is used to handle number of rows
for (int i=0; i<n; i++)
{
// inner loop to handles the number of spaces

for (int j=n-i; j>1; j--)
{
System.out.print(" ");
}

//This inner loop is used to handle number of columns
for (int j=0; j<=i; j++ )
{
System.out.print("* ");
}
// ending line after each row
System.out.println();
}
}

public static void main(String args[])
{
int n = 5; // Number of rows
print_pyramid(n);
}
}
Pyramid Program Output

The time complexity of the code is O(n2) and the space complexity is O(1).

2. Left Triangle Star Pattern

The Java code for printing a Left Triangle Star Pattern is given below:

  • Java

Java

import java.io.*;

// java program for left triangle
public class Ninja {

public static void Star_left_Triangle(int n)
{
int i, j;

// Outer loop to handle number of rows
for (i = 0; i < n; i++) {

// Loop to print spaces
for (j = 2 * (n - i - 1); j >= 0; j--) {
System.out.print(" ");
}

// Loop to print stars
for (j = 0; j <= i; j++) {
System.out.print("* ");
}

System.out.println();
}
}

public static void main(String args[])
{
int n = 5; // Number of Rows
Star_left_Triangle(n);
}
}
Left Triangle Start Pattern Output

The time complexity of the code is O(n2) and the space complexity is O(1).

3. Right Triangle Star Pattern

The Java code for printing a right triangle star pattern is given below:

  • Java

Java

import java.io.*;
public class Ninjas
{
public static void right_triangle(int n)
{
int i, j;

// outer loop is used to handle number of rows
for(i=0; i<n; i++)
{
// inner loop is used to handle number of columns
for(j=0; j<=i; j++)
{
System.out.print("* ");
}
// ending line after each row
System.out.println();
}
}

public static void main(String args[])
{
int n = 5; //Number of Rows
right_triangle(n);
}
}
Right Triangle Star Pattern Output

The time complexity of the code is O(n2) and the space complexity is O(1).

4. Diamond Shape Pattern Program

The Java code for printing a diamond shape pattern is given below:

  • Java

Java

import java.util.*;

class Ninja
{
static void print_Diamond(int n)
{
int sp = n - 1;

// run loop (parent loop) till number of rows
for (int i = 0; i < n; i++)
{
// loop for initial space
for (int j = 0; j < sp; j++)
System.out.print(" ");

for (int j = 0; j <= i; j++)
System.out.print("* ");

System.out.print("\n");
sp--;
}

// Repeat again to print in reverse order
sp = 0;

for (int i = n; i > 0; i--)
{
// loop for initial space
for (int j = 0; j < sp; j++)
System.out.print(" ");

for (int j = 0; j < i; j++)
System.out.print("* ");

System.out.print("\n");
sp++;
}
}

public static void main(String[] args)
{
int num=5;
print_Diamond(num);
}
}
Diamond Shape Pattern Program Output

The time complexity of the code is O(n2) and the space complexity is O(1).

5. Downward Triangle Star Pattern

The Java code for printing a downward triangle star pattern is given below:

  • Java

Java

import java.util.*;

class Ninja
{
static void print_Diamond(int n)
{
int sp = n - 1;

// run loop (parent loop) till number of rows
for (int i = 0; i < n; i++)
{
// loop for initial space
for (int j = 0; j < sp; j++)
System.out.print(" ");

for (int j = 0; j <= i; j++)
System.out.print("* ");

System.out.print("\n");
sp--;
}

// Repeat again to print in reverse order
sp = 0;

for (int i = n; i > 0; i--)
{
// loop for initial space
for (int j = 0; j < sp; j++)
System.out.print(" ");

for (int j = 0; j < i; j++)
System.out.print("* ");

System.out.print("\n");
sp++;
}
}

public static void main(String[] args)
{
int num=5;
print_Diamond(num);
}
}
Downward Triangle Star Pattern Output

The time complexity of the code is O(n2) and the space complexity is O(1).

6. Mirrored Right Triangle Star Pattern

The Java code for printing a mirrored right triangle star pattern is given below:

  • Java

Java

import java.io.*;

// java program for left triangle
public class Ninja {

public static void Star_left_Triangle(int n)
{
int i, j;

// Outer loop to handle number of rows
for (i = 0; i < n; i++) {

// Lopp to print spaces
for (j = 2 * (n - i - 1); j >= 0; j--) {
System.out.print(" ");
}

// Loop to print stars
for (j = 0; j <= i; j++) {
System.out.print("* ");
}

System.out.println();
}
}

public static void main(String args[])
{
int n = 5; // Number of Rows
Star_left_Triangle(n);
}
}
Mirrored Right Triangle Star Pattern Output

The time complexity of the code is O(n2) and the space complexity is O(1).

7. Reversed Pyramid Star Pattern

The Java code for printing a reversed pyramid star pattern is given below:

  • Java

Java

import java.io.*;

class Ninjas {

public static void main(String[] args)
{
int num = 7; // Size of the pyramid
int i = num, j;

// Outer loop
while (i > 0) {
j = 0;

while (j++ < num - i) {
// Print whitespaces
System.out.print(" ");
}
j = 0;

while (j++ < (i * 2) - 1) {
System.out.print("*");
}

// By now, we have reached the end of execution for one row
// so next line
System.out.println();
i--;
}
}
}
Reversed Pyramid Star Pattern Output

The time complexity of the code is O(n2) and the space complexity is O(1).

8. Right down Mirror Star Pattern

The Java code for printing a right-down mirror star pattern is given below:

  • Java

Java

import java.io.*;

public class Ninja
{
public static void main(String args[])
{

int num=6; //Number of rows
//This outer loop will run 6 times as number of rows is 8
for (int i=num; i>=1; i--)
{
//This inner for loop will print spaces in each row
for (int j=num; j>i;j--)
{
System.out.print(" ");
}
//This inner for loop will print stars in each row
for (int k=1;k<=i;k++)
{
System.out.print("*");
}

System.out.println();
}
}
}
Right down Mirror Star Pattern Output

The time complexity of the code is O(n2) and the space complexity is O(1).

9. Right Pascal’s Triangle

The Java code for printing a right Pascal's triangle is given below:

  • Java

Java

import java.io.*;
public class Ninjas
{
public static void main(String[] args)
{
int num= 6; //Number of rows

// There are two outer for loops in this program
// This is Outer Loop prints the first half of
// The Right Pascal triangle pattern
for (int i= 0; i<= num-1; i++)
{
for (int j=0; j<=i; j++)
{
System.out.print("*"+ " ");
}
System.out.println();
}
//This Outer Loop Prints second half of the triangle
for (int i=num-1; i>=0; i--)
{

for(int j=0; j <= i-1;j++)
{
System.out.print("*"+ " ");
}

System.out.println();
}
}
}
Right Pascal’s Triangle Output

The time complexity of the code is O(n2) and the space complexity is O(1).

10. Left Triangle Pascal’s

The Java code for printing a left Pascal's triangle is given below:

  • Java

Java

import java.io.*;
public class Ninjas
{
public static void main(String[] args)
{
int num= 6; //Number of rows

//There are two outer for loops in this program
//This Outer Loop prints the first half of
// the Left Pascal triangle pattern
for (int i= 1; i<=num; i++)
{

for (int j=i; j<num; j++)
{
System.out.print(" ");
}
//Prints the stars of each row
for (int k=1; k<=i;k++)
{
System.out.print("*");
}
System.out.println();
}
//This Outer Loop Prints second half of the triangle
for (int i=num; i>=1; i--)
{

for(int j=i; j <=num;j++)
{
System.out.print(" ");
}

for(int k=1; k<i ;k++)
{
System.out.print("*");
}

System.out.println();
}
}
}
Left Triangle Pascal’s Output

The time complexity of the code is O(n2) and the space complexity is O(1).

11. Sandglass Star Pattern

The Java code for printing a sandglass star pattern is given below:

  • Java

Java

import java.io.*;
public class Ninjas
{
public static void main(String[] args)
{
// Number of rows in the first and second half
// of Sandglass pattern
int num = 5;

//This outer loop Prints the first half of the pattern
for (int i= 0; i<= num-1 ; i++)
{

for (int j=0; j<i; j++)
{
System.out.print(" ");
}
//Prints stars and the whitespaces in between them
for (int k=i; k<=num-1; k++)
{
System.out.print("*" + " ");
}

System.out.println();
}
//This Outer loop Prints the second half of the pattern
for (int i= num-1; i>= 0; i--)
{

for (int j=0; j<i; j++)
{
System.out.print(" ");
}

for (int k=i; k<=num-1; k++)
{
System.out.print("*" + " ");
}

System.out.println();
}
}
}
Sandglass Star Pattern Output

The time complexity of the code is O(n2) and the space complexity is O(1).

12. Alphabet A Pattern

The Java code for printing Alphabet A pattern is given below:

  • Java

Java

import java.util.Scanner;
class Ninjas {
public static void display(int n)
{
// Outer for loop for number of lines
for (int i = 0; i < n; i++) {

for (int j = 0; j <= n / 2; j++) {

// prints two column lines
if ((j == 0 || j == n / 2) && i != 0 ||

// print first line of alphabet
i == 0 && j != 0 && j != n / 2 ||

// prints middle line
i == n / 2)

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

System.out.println();
}
}


public static void main(String[] args)
{
int num=7; //number of rows
display(num);
}
}
Alphabet A Pattern Output

The time complexity of the code is O(n2) and the space complexity is O(1).

13. Triangle Star Pattern

The Java code for printing the triangle star pattern is given below:

  • Java

Java

import java.util.Scanner;
public class Ninja
{
public static void main(String args[])
{
int n = 7; // Number of rows

//Outer Loop for number of Rows
for(int i=0;i<n;i++)
{
for(int k=i;k<n;k++)
System.out.print(" ");

// Run a loop till j = 2*i to print odd no. of stars.
for(int j=0;j<=2*i;j++)
{

if(j==0 || j==2*i || i==0 || i==n-1)
System.out.print("* ");
else
System.out.print(" ");
}

System.out.println();
}
}
}

 

Triangle Star Pattern Output

The time complexity of the code is O(n2) and the space complexity is O(1).

14. Down Triangle

The Java code for printing a down triangle pattern is given below:

  • Java

Java

public class Ninjas {

public static void main(String[] args)
{
int rows = 7;
for (int i = rows - 1; i >= 0; i--) {

// Inner loop for columns
for (int j = 0; j <= i; j++) {

// Prints star and space
System.out.print("*"
+ " ");
}

System.out.println();
}
}
}
Down Triangle Output

The time complexity of the code is O(n2) and the space complexity is O(1).

15. Diamond Star Pattern

The Java code for printing a diamond star pattern is given below:

  • Java

Java

import java.util.*;

class Ninja
{

static void print_Diamond(int n)
{
int sp = n - 1;

// run loop (parent loop) till number of rows
for (int i = 0; i < n; i++)
{
// loop for initial space
for (int j = 0; j < sp; j++)
System.out.print(" ");

for (int j = 0; j <= i; j++)
System.out.print("* ");

System.out.print("\n");
sp--;
}

// Repeat again to print in reverse order
sp = 0;

for (int i = n; i > 0; i--)
{
// loop for initial space
for (int j = 0; j < sp; j++)
System.out.print(" ");

for (int j = 0; j < i; j++)
System.out.print("* ");

System.out.print("\n");
sp++;
}
}

public static void main(String[] args)
{
int num=5;
print_Diamond(num);
}
}
Diamond Star Pattern Output

The time complexity of the code is O(n2) and the space complexity is O(1).

Numeric Patterns in Java

1. Simple Number Program

The Java code for printing a simple number program is given below:

  • Java

Java

import java.io.*;
public class Ninjas
{
public static void right_triangle(int n)
{
int i, j;

// outer loop is used to handle number of rows
for(i=0; i<n; i++)
{
// inner loop is used to handle number of columns
for(j=0; j<=i; j++)
{
System.out.print("1 ");
}
// ending line after each row
System.out.println();
}
}

public static void main(String args[])
{
int n = 5; //Number of Rows
right_triangle(n);
}
}
Simple Number Program Output

The time complexity of the code is O(n2) and the space complexity is O(1).

2. Number Pattern Program in Java

The Java code for printing a number pattern program is given below:

  • Java

Java

import java.util.Scanner;

public class Ninjas
{
public static void main(String[] args)
{
int rows=5; //Number of Rows
// Outer Loop handles the rows
for (int i = 1; i <= rows; i++)
{
// This loop handles the column values
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}

System.out.println();
}
}
}
Number Pattern Program Output

The time complexity of the code is O(n2) and the space complexity is O(1).

3. Diamond Pattern Program in Java

The Java code for printing the diamond pattern program is given below:

  • Java

Java

import java.util.Scanner;
class Ninjas {
public static void show(int n)
{
// sp stands for space
// st stands for number
int sp = n / 2, st = 1;

// This Outer loop for number of lines
for (int i = 1; i <= n; i++) {

// This Inner loop for printing space
for (int j = 1; j <= sp; j++)
System.out.print(" ");

// This Inner loop for printing number
int count = st / 2 + 1;
for (int k = 1; k <= st; k++) {
System.out.print(count);
if (k <= st / 2)
count--;
else
count++;
}

System.out.println();
if (i <= n / 2) {

sp = sp - 1;
st = st + 2;
}

else {

sp = sp + 1;
st = st - 2;
}
}
}

public static void main(String[] args)
{
int n=7;
show(n);
}
}
Diamond Pattern Program Output

The time complexity of the code is O(n2) and the space complexity is O(1).

4. Pascal’s Triangle Program in Java

The Java code for printing Pascal’s triangle pattern is given below:

  • Java

Java

import java.util.Scanner;
class Ninjas {
public static void show(int n)
{
// sp stands for space
// st stands for number
int sp = n / 2, st = 1;

// This Outer loop for number of lines
for (int i = 1; i <= n; i++) {

// This Inner loop for printing space
for (int j = 1; j <= sp; j++)
System.out.print(" ");

// This Inner loop for printing number
int count = st / 2 + 1;
for (int k = 1; k <= st; k++) {
System.out.print(count);
if (k <= st / 2)
count--;
else
count++;
}

System.out.println();
if (i <= n / 2) {

sp = sp - 1;
st = st + 2;
}

else {

sp = sp + 1;
st = st - 2;
}
}
}

public static void main(String[] args)
{
int n=7;
show(n);
}
}
Pascal's Triangle Program Output

The time complexity of the code is O(n2) and the space complexity is O(1).

5. Number Pattern Program in Java

The Java code for printing a number pattern program is given below:

  • Java

Java

class Ninjas {
public static void main(String args[]) {
int n = 5;
// Printing Upper Half for n rows
// This Loop is to iterate over each row in reverse order
for(int i=n;i>=1;i--){
for(int j=1;j<=i;j++){
System.out.print(j+ " ");
}
System.out.println();
}
// Printing Lower Half for n-1 rows
// This Loop is to iterate over each row

for(int i=2;i<=n;i++){
for(int j=1;j<=i;j++){
System.out.print(j+ " ");
}
System.out.println();
}
}
}
Number Program Pattern In Java output

The time complexity of the code is O(n2) and the space complexity is O(1).

6. Descending Order Pattern

The Java code for printing a descending-order pattern is given below:

  • Java

Java

public class Ninjas
{
public static void main(String[] args)
{
int rows = 5; //Number of rows

for (int i = 1; i <= rows; i++)
{
for (int j = rows; j >= i; j--)
{
System.out.print(j+" ");
}

System.out.println();
}
}
}
Descending Order Pattern Output

The time complexity of the code is O(n2) and the space complexity is O(1).

7. Binary Number Pattern

The Java code for printing a binary number pattern is given below:

  • Java

Java

public class Ninjas
{
public static void main(String[] args)
{

int rows = 5; //Number of rows
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
if(j%2 == 0)
{
System.out.print(0);
}
else
{
System.out.print(1);
}
}

System.out.println();
}

}
}
Binary Number Pattern Output

The time complexity of the code is O(n2) and the space complexity is O(1).

Must Read What are Loops in Java.

8. Right Triangle Numeric Pattern

The Java code for printing a right triangle numeric pattern is given below:

  • Java

Java

import java.util.Scanner;

public class Ninjas {
public static void main(String[] arg) {
int rows, i, j;
rows = 6;

// This Outer for loop prints one row in every iteration
for (i = 1; i <= rows; i++) {
// Inner for loop prints numbers of one row
for (j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.print("\n");
}
}
}
Right Triangle Numeric Pattern Output

The time complexity of the code is O(n2) and the space complexity is O(1).

9. Ones Pattern Program

The Java code for printing a ones pattern is given below:

  • Java

Java

public class Ninjas {

public static void main(String[] args) {

int n = 5; //Number of rows

// Outer loop for the row change
for(int i=1; i<=n; i++) {

// Inner loop1 for printing spaces
for(int j=1; j<=(n-i); j++) {

// Printing the space without changing the line
System.out.print(" ");
}

// Inner loop2 for printing ones
for(int k=1; k<=(2*i)-1; k++) {

// Printing the star without changing the line
System.out.print("1");
}
System.out.println();
}
}

}
Ones Pattern Program output

The time complexity of the code is O(n2) and the space complexity is O(1).

10. Diamond Numeric Pattern

The Java code for printing a diamond numeric pattern is given below:

  • Java

Java

public class Ninjas
{
public static void main(String[] args)
{
int row = 5; //Number of rows
//Outer Loop to handle number of rows for 1st half
for (int i = 1; i <= row; i++)
{
// This inner loop prints the spaces
for (int j = row - i; j >= 1; j--)
{
System.out.print(" ");
}

for (int j = 1; j <= 2 * i - 1; j++)
{
System.out.print(i);
}
System.out.println();
}
//This outer loop handles number of rows for lower half
for (int i = row - 1; i >= 1; i--)
{
for (int j = row - i; j >= 1; j--)
{
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++)
{
System.out.print(i);
}
System.out.println();
}
}
}
Diamond Numeric Pattern Output

The time complexity of the code is O(n2) and the space complexity is O(1).

Character Patterns In Java

Right Alphabetic Triangle

The Java code for printing a right alphabetic triangle is given below:

  • Java

Java

public class Ninjas
{
public static void main(String[] args)
{
int rows = 6; // Number of Rows
int alphabet = 65; // ASCII value of the first character

for (int i = 0 ; i < rows; i++ )
{
for (int j = 0 ; j <= i; j++ )
{
//ASCII value is incremented and the character with that value is printed
System.out.print((char)(alphabet + j) + " ");
}
System.out.println();
}
}
}
Right Alphabetic Triangle Output

The time complexity of the code is O(n2) and the space complexity is O(1).

Character Pattern Program

The Java code for printing a character pattern program is given below:

  • Java

Java

public class Ninjas
{
public static void main(String[] args)
{
int size = 6; //Number of rows
int ch = 65; // ASCII value of first character

for (int i = 0; i < size; i++) {
// printing spaces
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
// printing alphabets
for (int j = 0; j < size - i; j++) {
System.out.print((char)(ch+j));
}
System.out.println();

}
}
}
Character Pattern Program Output

The time complexity of the code is O(n2) and the space complexity is O(1).

K Shape Character Pattern

The Java code for printing K shape character pattern program is given below:

  • Java

Java

public class Ninjas {

public static void main(String[] args) {
int i, j, ch;

int rows = 6;
for (i = rows - 1; i >= 0; i-- )
{
ch = 65;
for (j = 0 ; j <= i; j++ )
{
System.out.print((char)(ch + j) + " ");
}
System.out.println();
}

for (i = 1 ; i < rows; i++ )
{
ch = 65;
for (j = 0 ; j <= i; j++ )
{
System.out.print((char)(ch + j) + " ");
}
System.out.println();
}
}
}
K Shape Character Pattern Output

The time complexity of the code is O(n2) and the space complexity is O(1).

Must Read Type Conversion in Java

Triangle Character Pattern

The Java code for printing the triangle character pattern program is given below:

  • Java

Java

public class Ninjas {
public static void main(String[] args) {
int rows = 6; //Number of rows
int ch = 65; //ASCII value of first character

for (int i = 0; i < rows; i++) {
// printing spaces
for (int j = 0; j < rows-i-1; j++) {
System.out.print(" ");
}
// printing alphabets
for (int k = 0; k < 2 * i + 1; k++) {
System.out.print((char)(ch+k));
}
System.out.println();
}
}
}
Triangle Character Pattern Output

The time complexity of the code is O(n2) and the space complexity is O(1).

Diamond Pattern

The Java code for printing the diamond pattern program is given below:

  • Java

Java

public class Ninjas {
public static void main(String[] args) {
int rows = 6; //Number of rows
int ch = 65; //ASCII value of first character

// upper pyramid
for (int i = 1; i <= rows; i++) {
// printing spaces
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
// printing alphabets
for (int k = 0; k < i * 2 - 1; k++) {
System.out.print((char)(ch+k));
}
System.out.println();
}

// lower pyramid
for (int i = 1; i <= rows - 1; i++) {
// printing spaces
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
// printing alphabets
for (int k = 0; k < (rows - i) * 2 - 1; k++) {
System.out.print((char)(ch+k));
}
System.out.println();
}
}
}
Diamond Pattern Output

The time complexity of the code is O(n2) and the space complexity is O(1).

Must Read: Java System Out Println

Frequently Asked Questions

Q. What is a pattern program in Java?

Pattern programs are simply patterns made up of numbers, alphabets, or symbols in a specific order. These pattern programs are simply solved using loop conditions. Pattern programs are the best way to learn how looping structures function in general-purpose programming languages.

Q. How many types of patterns are there in Java?

In Java, you may construct a variety of patterns, including but not limited to pyramid patterns, number patterns, star patterns, alphabet patterns, and diamond patterns. The possibilities are endless and can be realized by combining loops, conditional expressions, and print statements.

Q. What are the top 3 design patterns in Java?

The Singleton Pattern in Java assures that only one instance of a class exists, the Factory Pattern creates objects without specifying their classes, and the Observer Pattern allows for notice and response to changes in object states.

Q. How to find patterns in Java?

To find patterns in Java, you can use regular expressions (Regex) by importing the java.util.regex package. Define the pattern using Regex syntax and apply methods like matches() or find() to check if the pattern exists in a string or text.

Conclusion

In this article, we have extensively discussed pattern programs in Java. The article explains the different types of pattern programs in Java, namely star patterns, numeric patterns, and character patterns, with their program to print them. 

These types of programming questions are very important for interview preparation purposes.

Read more, Access modifiers in java

We hope that this blog has helped you enhance your knowledge regarding the pattern programs in Java, and if you would like to learn more, check out our articles on Java, String operations in Java, Regular Expressions in Java, and many more. 

Head over to our practice platform Code 360 to practice top problems, attempt mock tests, read interview experiences and interview bundles, follow guided paths for placement preparations, and much more!!

Happy Reading!!

Previous article
Bubble Sort in Java
Next article
What is Linear Search?
Live masterclass