Top 10 Pattern Programs in Java With Source Code For Beginners

Top 10 Pattern Programs in Java With Source Code For Beginners

Pattern programs in java

Top 10 Pattern Programs in Java With Source Code For Beginners

Top 10 Pattern Programs in Java With Source Code For Beginners

Top 10 Pattern Programs in Java With Source Code For Beginners

Telegram Group Join Now
WhatsApp Channel Join Now
YouTube Channel Subscribe

Top 10 Pattern Programs in Java With Source Code For Beginners

Introduction

If you are new to Java Programming or in any programming language, understanding pattern programs is very important to improve your coding skills. Pattern programs in Java are simply programs that generate patterns of characters, numbers, or symbols in a structured format. 

These programs help in understanding loops, nested loops, and the logic building aspects of programming. 

In technical round most interviewers ask pattern program questions because they test a candidate’s knowledge of basic programming concepts like loops, conditionals, and nested loops. Pattern programs are a simple way to see how well someone can write and understand code.

In this blog we cover all types of pattern programs in Java to help you build a solid foundation in programming. 

Let’s explore some of the most common and interesting patterns with source code & we also explain the logic behind the code that might be helpful for your learning journey.


Enroll now and take the first step towards a successful career. Click here to join our courses today!


Pattern Programs in Java

There are three types of Pattern Program in Java Programming – 

  1. Star Patterns
  2. Number Patterns
  3. Character Patterns

let’s explore each pattern program with detailed logic, one by one – 

Pattern Programs in Java – Star Patterns 

1) Pyramid Star Pattern – 

    * 

   * * 

  * * * 

 * * * * 

* * * * * 

Program – 

 

Logic of Program – 

Outer Loop (i loop):

  • Iterates through each row from 0 to rows-1.
  • i determines the current row number.

First Inner Loop (j loop for spaces):

  • Prints spaces before the stars.
  • Decreases the number of spaces as “i” increases to center-align the stars.

Second Inner Loop (j loop for stars):

  • Prints stars in each row.
  • Increases the number of stars as i increases.

Also Read – What is Java Programming


2) Inverted Pyramid Star Pattern – 

* * * * * 

 * * * * 

  * * * 

   * * 

    * 

Program – 

 

Logic of Program – 

Outer Loop (for (int i = rows; i >= 1; i–) { … }):

  • Iterates through each row in reverse order, starting from rows down to 1.
  • Controls the number of rows in the inverted pyramid.

First Inner Loop (for (int j = 0; j < rows – i; j++) { … }):

  • Prints spaces before the stars.
  • rows – i calculates the number of spaces needed to align the stars properly for each row.

Second Inner Loop (for (int j = 1; j <= i; j++) { … }):

  • Prints the stars in each row.
  • Starts from 1 and continues until i, which determines the number of stars in the current row.

Also Read –  Java Interview Questions


3) Right-angled Triangle Star Pattern –

* * 

* * * 

* * * * 

* * * * * 

Program – 

 

 

Logic of Program – 

Outer Loop (for (int i = 0; i < rows; i++) { … }):

  • Iterates through each row from 0 to rows-1.
  • Controls the number of rows in the right-angled triangle.

Inner Loop (for (int j = 0; j <= i; j++) { … }):

  • Prints stars in each row.
  • j iterates from 0 to i, printing one star per iteration to form the triangle shape.

4) Hollow Diamond Star Pattern

    *

   * *

  *   *

 *     *

*       *

 *     *

  *   *

   * *

    *

 

Program – 

 

Logic Of Program – 

Upper Part of Diamond:

  • Outer Loop (for (int i = 0; i < rows; i++) { … }):
  • Iterates through each row from 0 to rows-1 for the top half of the diamond.
  • First Inner Loop (for (int j = 0; j < rows – i – 1; j++) { … }):
  • Prints spaces before the stars to center-align them.
  • Second Inner Loop (for (int j = 0; j < 2 * i + 1; j++) { … }):
  • Prints stars and spaces. It prints a star (*) only at the beginning and end (j == 0 || j == 2 * i) to form the outline of the diamond shape.

Lower Part of Diamond (Inverted):

  • Uses a similar structure as the upper part but in reverse (for (int i = rows – 2; i >= 0; i–) { … }).
  • Repeat the process to print the bottom half of the diamond shape.

Pattern Programs in Java – Number Patterns

5) Right-angled Triangle

2 3 

4 5 6 

7 8 9 10 

11 12 13 14 15 

 

Program – 

 

Logic Of Program – 

Outer Loop (for (int i = 1; i <= rows; i++) { … }):

  • Iterates through each row of the pattern, from 1 to rows.

Inner Loop (for (int j = 1; j <= i; j++) { … }):

  • Prints numbers in each row.
  • Starts from 1 and continues up to the current row number i.

Printing:

  • Prints the current number (number) followed by a space.
  • Increments number after each print to prepare for the next number.

Output:

  • Each row starts with the next sequential number, forming a triangular pattern of numbers.

6) Pyramid 

        1   

      2   3   

    4   5   6   

  7   8   9   10   

11   12   13   14   15   

 

Program – 

 

Logic Of Program –

Initialization:

  • int rows = 5;: Specifies the number of rows in the pattern. In this example, it’s set to 5.
  • int number = 1;: Initializes the starting number for the pattern. This starts at 1.

Outer Loop (for (int i = 1; i <= rows; i++) { … }):

  • This loop iterates through each row of the pattern.
  • It starts from 1 and continues until i reaches rows (which is 5 in this case).

First Inner Loop (for (int j = 1; j <= rows – i; j++) { … }):

  • Inside the outer loop, this loop controls the number of spaces printed before the numbers in each row.
  • It prints rows – i spaces for each row to right-align the numbers.

Second Inner Loop (for (int j = 1; j <= i; j++) { … }):

  • Also inside the outer loop, this loop prints the numbers in each row.
  • It starts from 1 and continues up to i, printing the current value of number followed by three spaces (number + ” “).
  • After printing each number, number is incremented (number++) to prepare for the next number in the sequence.

7) Inverted Pyramid

11   12   13   14   15   

    7   8   9   10   

      4   5   6   

        2   3   

          1   

 

Program – 

 

Logic Of Program – 

 

Initialization:

  • int rows = 5;: Specifies the number of rows in the pattern. In this example, it’s set to 5.
  • int number = 1;: Initializes the starting number for the pattern. This starts at 1.

Outer Loop (for (int i = rows; i >= 1; i–) { … }):

  • This loop iterates through each row of the pattern in reverse order.
  • It starts from rows (which is 5 in this case) and continues until i is 1.

First Inner Loop (for (int j = 0; j < rows – i; j++) { … }):

  • Inside the outer loop, this loop controls the number of spaces printed before the numbers in each row.
  • It prints rows – i spaces for each row to right-align the numbers.

Second Inner Loop (for (int j = 1; j <= i; j++) { … }):

  • Also inside the outer loop, this loop prints the numbers in each row.
  • It starts from 1 and continues up to i, printing the current value of number followed by three spaces (number + ” “).
  • After printing each number, number is incremented (number++) to prepare for the next number in the sequence.

Also Read –  Swapping Of two numbers in Java


Pattern Programs in Java – Character Patterns 

8) Right-Angle Triangle Pattern of Characters

A

A B

A B C

A B C D

A B C D E

 

Program – 

 

 

Logic Of Program – 

 

Step 1 – First you have to declare the number of rows (‘n’). This value determines how many rows of the pattern will be printed. 

For example – if ‘n’ is set to 5, the pattern will have 5 rows. 

 

Step 2 – Use an outer loop to iterate through each row. This loop runs from 1 to ‘n’ (the total number of rows).

For example –  for (int i = 1; i <= n; i++)

 

Step 3 – At the beginning of each row, initialize a character variable to ‘A’. So this makes sure that each row starts printing from the letter ‘A’.

For Example – char ch = ‘A’;

 

Step 4 – Inner Loop for Columns

  • Within the outer loop, use an inner loop to control the number of characters printed in the current row.
  • The inner loop runs from 1 to the current row number (i). This means that in the first row, the loop runs once; in the second row, it runs twice, and so on.
  • The variable j is used to keep track of the current column number within the row.

For Example –  for (int j = 1; j <= i; j++)

 

Step 5 –  Prints the current character and then increments to the next character.

For Example – 

System.out.print(ch + ” “);

Ch++;


9) Inverted Right-Angle Triangle Pattern of Characters

A B C D E

A B C D

A B C

A B

A

Program – 

 

Logic Of Program – 

 

Step 1 – The first step is to declare the number of rows (n).

For example –   int n = 5;

 

Step 2 – 

  • The outer loop iterates from n down to 1. This loop controls the number of rows and starts from the maximum number of rows (n) and decreases by 1 with each iteration.
  • The variable i is used to keep track of the current row number.

For Example –   for (int i = n; i >= 1; i–)

 

Step 3 – Initialize Character for Each Row

For Example –  char ch = ‘A’

 

Step 4 – Inner Loop for Columns

  • Within the outer loop, use an inner loop to control the number of characters printed in the current row.
  • The inner loop runs from 1 to the current row number (i). This means that in the first iteration of the outer loop (when i is n), the inner loop runs ‘n’ times; in the second iteration, it runs ‘n-1’ times, and so on.
  • The variable ‘j’ is used to keep track of the current column number within the row.

For Example –   for (int j = 1; j <= i; j++) 

 

Step 5 –   Print the current character and then move to the next character.

For Example –

System.out.print(ch + ” “);

ch++;


10) Pyramid Pattern of Characters

    A

   A B

  A B C

 A B C D

A B C D E

 

Program – 

 

Logic Of Program – 

Step 1 – The first step is to declare the number of rows (n).

For Example –   int n = 5

 

Step 2 –

  • Use an outer loop to iterate through each row. This loop runs from 1 to ‘n’. Each iteration of this loop corresponds to one row in the pattern.
  • The variable ‘i’ is used to keep track of the current row number.

For Example –  for (int i = 1; i <= n; i++)

 

Step 3 –

  • Before printing the characters for each row, print the leading spaces to create the pyramid shape.
  • This inner loop runs from i to n-1. It prints spaces to center-align the characters in the current row.
  • The variable j is used to keep track of the number of spaces.

For Example – for (int j = i; j < n; j++)

 

Step 4 – Initialize Character for Each Row

For Example – char ch = ‘A’

 

Step 5 – Inner Loop for Characters

  • Use another inner loop to control the number of characters printed in the current row.
  • This loop runs from 1 to the current row number (i). The variable k is used to keep track of the number of characters in the current row.
  • Inside this loop, print the current character (ch) followed by a space. This creates the required spacing between characters in the pattern.
  • After printing the current character, increment the character variable (ch) to the next character in the alphabet.

For Example – 

for (int k = 1; k <= i; k++) 

{ System.out.print(ch + ” “); 

ch++


Also Read – How To Reverse A Number In Java


11) Diamond Pattern of Characters

    A

   A B

  A B C

 A B C D

A B C D E

 A B C D

  A B C

   A B

    A

 

Program –

 

Logic Of Program –

Upper Part (Including the Middle Row)

Step 1 – The first step is to declare the number of rows (n).

For Example –  int n = 5;

 

Step 2 – 

  • Use an outer loop to iterate through each row of the upper part. This loop runs from 1 to ‘n’. Each iteration of this loop corresponds to one row in the upper part of the diamond.
  • The variable ‘i’ is used to keep track of the current row number.

For Example –  

        for (int i = 1; i <= n; i++) 

               for (int j = i; j < n; j++)

Step 3 – 

  • Before printing the characters for each row, print leading spaces to create the pyramid shape.
  • This inner loop runs from ‘i’ to ‘n-1’. It prints spaces to center-align the characters in the current row.
  • The variable ‘j’ is used to keep track of the number of spaces.

For Example –  

for (int i = 1; i <= n; i++) 

for (int j = i; j < n; j++) 

 

Step 4 – Initialize Character for Each Row

For Example – char ch = ‘A’

 

Step 5 – Inner Loop for Characters: Control the number of characters printed in the current row.

For Example –   for (int k = 1; k <= i; k++)

 

Step 6 – Print the current character and then move to the next character.

For Example –  

System.out.print(ch + ” “);

ch++;

Lower Part (Excluding the Middle Row)

Step 1 – Outer Loop:

  • Iterates from n-1 down to 1.
  • This loop determines the number of rows in the lower part.

Step 2 – Leading Spaces:

  • Printed by an inner loop that iterates from ‘n’ to i+1.
  • The number of spaces increases as ‘i’ decreases, centering the characters to form the bottom half of the diamond.

Step 3 – Character Printing:

  • Characters are printed by an inner loop that iterates from 1 to i.
  • The number of characters decreases with each row, starting from ‘A’ and incrementing.

Want to learn Java Programming from industry Experts?

Contact Us For more information. 

Prince

Prince

Leave a Reply

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

Blogs You May Like

Get in touch to claim Best Available Discounts.

If You Are Looking for Job Assistance Please Fill Up the Form.

× How can I help you?