Top 25 Pattern Programs in Java For Printing Numbers

Java is the most popular programming language recognized for its simplicity and versatility. Wide range of applications can also be developed using it, including web, mobile, and desktop applications. In addition, Java offers powerful tools for developers to create complex programs easily and efficiently. One of the most promising features of Java is its ability to create pattern programs that can print numbers in a specific format. Pattern programs are an excellent way to learn Java programming, especially for beginners, since it helps in understanding the syntax and logic of Java programming.

Java Pattern Programs have always been one of the critical parts of the Java Interview questions. They look almost impossible to crack at a point, but these questions are practically based on mathematical logic and matrices' fundamentals. Hence Java Pattern Programs are greatly sought-after.

This Java Pattern Programs article covers almost every possible type of pattern programs that will give you a better understanding of the logic to decode the pattern and become capable of building one in your interview.

How to Print Pattern in Java?

Printing patterns in Java is a common task in programming, especially in the early stages of learning. Patterns are printed by arranging symbols or numbers in a specific way to form a design or shape. These patterns are often used in problem-solving and can be useful in developing algorithmic thinking skills. This article will discuss how to print patterns in Java and explore some of the most common patterns.

Loops and control statements to print patterns in Java are best. The loops help you to repeat a block of code until a certain condition is met, and the control statements allow you to alter the flow of the program based on certain conditions. The different patterns in Java are discussed below:

We will deal with different types of Java Pattern Programs through the following docket.

Pattern Programs in Java

Star Patterns in Java 

Star patterns are a popular pattern program in Java, often used to create interesting visual designs or graphics. These programs use asterisks or other symbols to create various shapes and patterns. Star patterns are commonly used in computer graphics, logo designs, and other visual displays.

Creating star patterns in Java involves using nested loops to control the number of rows and columns and the position of the asterisks or other symbols. The program can be customized to create patterns, including triangles, squares, circles, and more complex designs. Also, it can be customized to create a variety of patterns, as mentioned below:

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Pattern 1

java-pattern-programs-P1

/*Star Pattern 1

* * 

* * * 

* * * * 

* * * * *   */

package Patterns;

public class Star {

public static void main(String[] args) {

int rows = 5;

for (int i = 1; i <= rows; ++i) {  //Outer loop for rows

for (int j = 1; j <= i; ++j) { //Inner loop for Col

System.out.print("* "); //Print *

}

System.out.println(); //New line

}

}

}

Pattern 2

java-pattern-programs-P2

/*Star Pattern 2

* * * * * 

* * * * 

* * * 

* * 

*   */

package Patterns;

public class Star {

    public static void main(String[] args) {

        int rows = 5;

        for(int i = rows; i >= 1; --i) {  //For Loop for Row 

            for(int j = 1; j <= i; ++j) { //For Loop for Col

                System.out.print("* "); //Prints *

            }

            System.out.println(); //Get to newline

        }

    }

}

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Pattern 3

java-pattern-programs-P3

/*Star Pattern 3

* * 

* * * 

* * * * 

* * * * * 

* * * * 

* * * 

* * 

*   */

package Patterns;

import java.util.Scanner;

public class Star {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in); //Input

System.out.println("Enter the number of rows: ");

int rows = sc.nextInt();

for (int i = 0; i <= rows - 1; i++) { //For Loop for Row 

for (int j = 0; j <= i; j++) { //For Loop for Col

System.out.print("*" + " "); //prints * and blank space

}

System.out.println(""); // new line

}

for (int i = rows - 1; i >= 0; i--) { //For Loop for Row

for (int j = 0; j <= i - 1; j++) { //For Loop for Col

System.out.print("*" + " "); //prints * and blank space

}

System.out.println("");// new line

}

sc.close();

}

}

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Pattern 4

java-pattern-programs-P4

/*Star Pattern 4

           * 

         * * 

       * * * 

     * * * * 

   * * * * *   */

package Patterns;

public class Star {

public static void printStars(int n) {

int i, j;

for (i = 0; i < n; i++) {  

for (j = 2 * (n - i); j >= 0; j--) { //For Loop for Row

System.out.print(" "); // Print Spaces

}

for (j = 0; j <= i; j++) { //For Loop for col

System.out.print("* "); // Print Star

}

System.out.println();

}

}

public static void main(String args[]) {

int n = 5; //Number of Rows

printStars(n);

}

}

Preparing Your Blockchain Career for 2024

Free Webinar | 5 Dec, Tuesday | 9 PM ISTRegister Now
Preparing Your Blockchain Career for 2024

Pattern 5

java-pattern-programs-P5

/*Star Pattern 5

* * * * *

  * * * *

    * * *

      * *

        *  */

package Patterns;

import java.util.Scanner;

public class Star {

public static void main(String[] args) {

Scanner S = new Scanner(System.in); //Input

System.out.println("Enter row value ");

int r = S.nextInt();

for (int i = r; i >= 1; i--) { 

for (int j = r; j > i; j--) { 

System.out.print(" "); // Prints Blank space

}

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

System.out.print("*"); //Prints *

}

System.out.println(" "); //Prints blank spaces

}

S.close();

}

}

Pattern 6

java-pattern-programs-P6

/*Star Pattern 6

    *

   **

  ***

 ****

*****

 ****

  ***

   **

    *   */

package Patterns;

import java.util.Scanner;

public class Star {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of rows: ");

int rows = sc.nextInt();

for (int i = 1; i <= rows; i++) {

for (int j = i; j < rows; j++) { //Rows Loop

System.out.print(" "); // Blank Space

}

for (int k = 1; k <= i; k++) { //Cols Loop

System.out.print("*"); // Prints *

}

System.out.println("");

}

for (int i = rows; i >= 1; i--) {

for (int j = i; j <= rows; j++) { //Rows Loop

System.out.print(" ");  // Prints blank spaces

}

for (int k = 1; k < i; k++) { //Col Loop

System.out.print("*");  // Prints *

}

System.out.println(""); // New Line1

}

sc.close();

}

}

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Pattern 7

java-pattern-programs-P7

/*Star Pattern 7

    * 

   * * 

  * * * 

 * * * * 

* * * * *    */

package Patterns;

public class Star {

public static void printTriagle(int n) {

for (int i = 0; i < n; i++) {

for (int j = n - i; j > 1; j--) { //Loop for blank space

System.out.print(" "); //Print Space

}

for (int j = 0; j <= i; j++) { loop for star

System.out.print("* "); //Print Star

}

System.out.println(); //Newline

}

}

public static void main(String args[]) {

int n = 5;

printTriagle(n);

}

}

Pattern 8

java-pattern-programs-P8.

/*Star Pattern 8

 * * * * * 

  * * * * 

   * * * 

    * * 

     *  */

package Patterns;

import java.util.Scanner;

public class Star {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of rows: ");

int rows = sc.nextInt();

for (int i = 0; i <= rows - 1; i++) { //For loop for Rows

for (int j = 0; j <= i; j++) { //For loop for Col

System.out.print(" "); // blank space

}

for (int k = 0; k <= rows - 1 - i; k++) { 

System.out.print("*" + " "); // prints * and blank space

}

System.out.println(); //Next line

}

sc.close();

}

}

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Pattern 9

java-pattern-programs-P9

/*Star Pattern 9

    *

   ***

  *****

 *******

*********

 *******

  *****

   ***

    *     */

package Patterns;

import java.util.Scanner;

public class Star {

public static void main(String args[]) {

int n, x, j, blank = 1;

System.out.print("Enter the value for rows: ");

Scanner s = new Scanner(System.in);

n = s.nextInt(); //input

blank = n - 1; // logic for balck spaces 

//Upper star Pyramid

for (j = 1; j <= n; j++) {

for (x = 1; x <= blank; x++) {

System.out.print(" "); //print blank space

}

blank--;

for (x = 1; x <= 2 * j - 1; x++) {

System.out.print("*"); //Print Star

}

System.out.println("");

}

//Lower star Pyramid

blank = 1;

for (j = 1; j <= n - 1; j++) {

for (x = 1; x <= blank; x++) {

System.out.print(" "); //Print Spaces

}

blank++;

for (x = 1; x <= 2 * (n - j) - 1; x++) {

System.out.print("*"); //Print Star

}

System.out.println(""); //Print new line

}

}

}

Pattern 10

java-pattern-programs-P10

/*Star Pattern 10

* * * * * 

 * * * * 

  * * * 

   * * 

    * 

    * 

   * * 

  * * * 

 * * * * 

* * * * *   */

package Patterns;

import java.util.Scanner;

public class Star {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of rows: ");

int rows = sc.nextInt(); //Input

//Upper Inverted Pyramid

for (int i = 0; i <= rows - 1; i++) {

for (int j = 0; j < i; j++) {

System.out.print(" "); Print blank space

}

for (int k = i; k <= rows - 1; k++) {

System.out.print("*" + " "); //Print star and blank space

}

System.out.println(""); //New line

}

//For lower Pyramid

for (int i = rows - 1; i >= 0; i--) {

for (int j = 0; j < i; j++) {

System.out.print(" "); //Print spaces

}

for (int k = i; k <= rows - 1; k++) {

System.out.print("*" + " "); //Print Star and Space

}

System.out.println(""); //Print New line

}

sc.close();

}

}

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Pattern 11

java-pattern-programs-P11

/*Diagonal 11

        *

      *

    *

  *

*             */

package Patterns;

public class Star {

public static void main(String[] args) {

int i, j;

for (i = 1; i <= 5; i++) {

for (j = 0; j < 5 - i; j++) {

System.out.print("  "); //Print blank space

}

System.out.print("*"); //Print Star and newline

}

}

}

X-pattern

It creates a diagonal pattern of X characters. This pattern can be created with a nested loop that prints X characters in specific positions based on the row and column numbers.

Pattern 12

java-pattern-programs-P12

/*X Pattern 12

*     *

 *   *

  * *

   *

  * *

 *   *

*     *  */

package Patterns;

import java.util.*;

public class Star {

public static void main(String args[]) {

int i, j, n;

Scanner sc = new Scanner(System.in);

System.out.println("Enter a value for n");

n = sc.nextInt(); //Input

//Upper V pattern

for (i = n; i >= 1; i--) {

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

System.out.print(" ");//print spaces

}

for (j = 1; j <= (2 * i - 1); j++) {

if (j == 1 || j == (2 * i - 1))//Logic for printing star

System.out.print("*");

else

System.out.print(" ");//if logic fails print space

}

System.out.println("");

}

//Lower Inverted V pattern

for (i = 2; i <= n; i++) {

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

System.out.print(" ");//Print spaces

}

for (j = 1; j <= (2 * i - 1); j++) {

if (j == 1 || j == (2 * i - 1))//Logic for printing star

System.out.print("*");

else

System.out.print(" ");//if logic fails print space

}

System.out.println("");

}

}

}

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Pattern 13

java-pattern-programs-P13

/*Inverted V 13

    *     

   * *    

  *   *   

 *     *  

*       *   */

package Patterns;

import java.util.Scanner;

public class Star {

public static void main(String[] args) {

Scanner cs = new Scanner(System.in); //Input

System.out.println("Enter the row size:");

int out, in;

int row_size = cs.nextInt();

int print_control_x = row_size;

int print_control_y = row_size;

for (out = 1; out <= row_size; out++) {

for (in = 1; in <= row_size * 2; in++) {

if (in == print_control_x || in == print_control_y) {

System.out.print("*");

} else {

System.out.print(" ");

}

}

print_control_x--;

print_control_y++;

System.out.println();

}

cs.close();

}

}

Pattern 14

java-pattern-programs-P14

/* V-pattern

*       *

 *     *

  *   *

   * *

    *     */

package Patterns;

public class Star {

static void pattern(int n) {

int i, j;

for (i = n - 1; i >= 0; i--) {

for (j = n - 1; j > i; j--) {

System.out.print(" "); //Print Space

}

System.out.print("*"); //Print star

for (j = 1; j < (i * 2); j++)

System.out.print(" ");//Print space

if (i >= 1)

System.out.print("*");

System.out.print("");//Enter newline

}

}

public static void main(String args[]) {

int n = 5;

pattern(n); //Pattern method call

}

}

Pattern 15

java-pattern-programs-P15

/*Rombus 15

    *

   * *

  *   *

 *     *

*       *

 *     *

  *   *

   * *

    *     */

package Patterns;

import java.util.Scanner;

public class Star {

public static void main(String[] args) {

System.out.println("Number of rows: ");

int rows = extracted().nextInt();

int i;

//upper inverted V pattern

for (i = 1; i <= rows; i++) {

for (int j = rows; j > i; j--) {

System.out.print(" "); //Print spaces

}

System.out.print("*"); //Print Spaces

for (int k = 1; k < 2 * (i - 1); k++) { /Logic for pattern

System.out.print(" "); //Print Spaces

}

if (i == 1) {

System.out.println(""); //Condition true, go to newline

} else {

System.out.println("*"); //else print star

}

}

//Lower v pattern

for (i = rows - 1; i >= 1; i--) {

for (int j = rows; j > i; j--) {

System.out.print(" "); //Print Spaces

}

System.out.print("*");

for (int k = 1; k < 2 * (i - 1); k++) { Logic for pattern

System.out.print(" ");

}

if (i == 1)

System.out.println(""); //newline

else

System.out.println("*"); //Print star

}

}

private static Scanner extracted() {

return new Scanner(System.in);

}

}

Pattern 16

java-pattern-programs-P16

/*Star Pattern 16

    *

   * *

  *   *

 *     *

*********   */

package Patterns;

import java.util.Scanner;

public class Star {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of rows: ");

int rows = sc.nextInt();

for (int i = 1; i <= rows; i++) {

for (int j = i; j < rows; j++) {

System.out.print(" ");

}

for (int k = 1; k <= (2 * i - 1); k++) {

if (k == 1 || i == rows || k == (2 * i - 1)) {

//Logic for printing Pattern

System.out.print("*"); //Print Star

} else {

System.out.print(" ");  //Print Spaces

}

}

System.out.println("");

}

sc.close();

}

}

Become a Certified UI UX Expert in Just 5 Months!

UMass Amherst UI UX BootcampExplore Program
Become a Certified UI UX Expert in Just 5 Months!

Pattern 17

java-pattern-programs-P17

/*Star Pattern 17

*********

 *     *

  *   *

   * *

    *      */

package Patterns;

import java.util.Scanner;

public class Star {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of rows: ");

int rows = sc.nextInt(); //Row input

for (int i = rows; i >= 1; i--) {

for (int j = i; j < rows; j++) {

System.out.print(" "); //Print Spaces

}

for (int k = 1; k <= (2 * i - 1); k++) {

if (k == 1 || i == rows || k == (2 * i - 1)) { //logic to print Pattern

System.out.print("*"); //Ture prints star

} else {

System.out.print(" "); //False prints spaces

}

}

System.out.println("");

}

sc.close();

}

}

Pattern 18

java-pattern-programs-P18.

/*Box 18

**********

*        *

*        *

*        *

*        *

*        *

*        *

*        *

*        *

**********  */

package Patterns;

public class Star {

static void print_rectangle(int n, int m) {

int i, j;

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

for (j = 1; j <= m; j++) {

if (i == 1 || i == n || j == 1 || j == m) //Logic to print 

System.out.print("*"); //Tue?, print star

else

System.out.print(" "); //Tue?, print space

}

System.out.println();

}

}

public static void main(String args[]) {

int rows = 10, columns = 10;

print_rectangle(rows, columns); //Method call

}

}

Pattern 19

java-pattern-programs-P19

1 2 

1 2 3 

1 2 3 4 

1 2 3 4 5 */ 

package Patterns;

import java.util.Scanner;

public class Star {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in); //Input

System.out.println("Number of rows: ");

int rows = sc.nextInt(); 

for (int i = 1; i <= rows; i++) {

for (int j = 1; j <= i; j++) {

System.out.print(j + " "); //Print j value and space

}

System.out.println();

}

sc.close();

}

}

Floyd's Triangle Pattern

This pattern is created by printing a triangle of numbers, starting from 1 and incrementing the value by 1 for each row. However, the numbers are printed in a specific order, as shown in the example below:

Pattern 20

java-pattern-programs-P20

/*Number Pattern 20 (Floyd's Triangle)

2 3 

4 5 6 

7 8 9 10 

11 12 13 14 15  */

package Patterns;

public class Star {

public static void main(String[] args) {

int i, j, k = 1;

for (i = 1; i <= 5; i++) {

for (j = 1; j < i + 1; j++) {

System.out.print(k++ + " "); /Floyd’s triangle logic(prints K value and increments on every iteration)

}

System.out.println();

}

}

}

Pascal's Triangle 

It involves creating a triangular pattern of numbers that follow a specific mathematical rule. This pattern can be created with a nested loop that calculates the value of each number based on its position in the triangle.

Pattern 21

java-pattern-programs-P21

/*Number Pattern 21 (Pascal's Triangle)

               1

             1   1

           1   2   1

         1   3   3   1

       1   4   6   4   1

     1   5  10  10   5   1 */

package Patterns;

public class Star {

public static void main(String[] args) {

int x = 6;

for (int i = 0; i < x; i++) {

int num = 1;

System.out.printf("%" + (x - i) * 2 + "s", ""); //Pascals triangle logic

for (int j = 0; j <= i; j++) {

System.out.printf("%4d", num);

num = num * (i - j) / (j + 1);

}

System.out.println();

}

}

}

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Numeric Patterns

It is another type of Java pattern program that involves printing numbers in a specific sequence or arrangement. These programs can be used to create tables, graphs, and other types of visual displays.

In Java, creating numeric patterns involves using loops for controlling the number of rows and columns and the value of the numbers printed. The program can be customized to create patterns, including multiplication tables, Fibonacci sequences, and more complex designs.

Pattern 22

java-pattern-programs-P22

/*Number Pattern 22

10101

01010

10101

01010

10101  */

package Patterns;

import java.util.Scanner;

public class Star {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Number of rows: ");

int rows = sc.nextInt(); //Input

for (int i = 1; i <= rows; i++) {

int num;

if (i % 2 == 0) {

num = 0;

for (int j = 1; j <= rows; j++) {

System.out.print(num);

num = (num == 0) ? 1 : 0;

}

} else {

num = 1;

for (int j = 1; j <= rows; j++) {

System.out.print(num);

num = (num == 0) ? 1 : 0;

}

}

System.out.println();

}

sc.close();

}

}

Pattern 23

java-pattern-programs

/*Ruby Pattern 23

   A

  B B

 C   C

D     D

 C   C

  B B

   A     */

package Patterns;

import java.util.Scanner;

public class Ruby {

public static void main(String[] args) {

char[] alpha = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',

'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

int digit = 0;

String[] ruby = new String[26];

System.out.print("Input Uppercase alphabet between A to Z:");

Scanner reader = new Scanner(System.in);

try {

char aplhabet = reader.next("[A-Z]").charAt(0);

for (int i = 0; i < alpha.length; i++) {

if (alpha[i] == aplhabet) {

digit = i;

break;

}

}

for (int i = 0; i <= digit; i++) {

ruby[i] = "";

for (int p = 0; p < digit - i; p++) {

ruby[i] += " ";

}

ruby[i] += alpha[i];

if (alpha[i] != 'A') {

for (int p = 0; p < 2 * i - 1; p++) {

ruby[i] += " ";

}

ruby[i] += alpha[i];

}

System.out.println(ruby[i]);

}

for (int i = digit - 1; i >= 0; i--) {

System.out.println(ruby[i]);

}

} catch (Exception e) { //Exception

e.printStackTrace();

} finally {

reader.close();

}

}

}

Pattern 24

java-pattern-programs-P24

/*Alphabet Pattern 24

A B 

A B C 

A B C D 

A B C D E 

A B C D E F   */

package Patterns;

public class Alphabet {

public static void main(String[] args) {

int alphabet = 65; //ASCII value of “A”

for (int i = 0; i <= 5; i++) {

for (int j = 0; j <= i; j++) {

System.out.print((char) (alphabet + j) + " "); //Logic to print Alphabet pattern

}

System.out.println();

}

}

}

Pattern 25

java-pattern-programs-P25

/*Alphabet Pattern 25

    A 

   A B 

  A B C 

 A B C D 

A B C D E  */

package Patterns;

public class Alphabet {

public static void main(String[] args) {

for (int i = 0; i <= 4; i++) {

int alphabet = 65; //ASCII value of “A”

for (int j = 4; j > i; j--) {

System.out.print(" "); //Print Space

}

for (int k = 0; k <= i; k++) {

System.out.print((char) (alphabet + k) + " "); //Print Alphabet

}

System.out.println();

}

}

}

Conclusion

Java Pattern Programs are useful in enhancing the analytical capabilities of a Java Developer. Practicing them will not only help you to crack the interviews but also to decode the critical, logical issues in your code.

Followed by Java Pattern Programs, you can also go through some most frequently asked Java Interview Questions tutorial so that you can be interview ready at all times.

If you're looking for more in-depth knowledge about the Java programming language and how to get certified as a professional developer, explore our Java training and certification programs. Simplilearn's qualified industry experts offer the same in real-time experience. In particular, check out our Full Stack Java Developer Career Bootcamp today!

If you have any queries regarding this "Java Pattern Programs" article, please leave them in the comments section, and our expert team will solve them for you at the earliest!

Himanshu Sukhija started his career 6 years ago with TCS in a QA role. But he was always interested in software development. When he reached out to his HR team for a role change, they suggested pursuing a professional course. He found Java courses offered by Simplilearn and finalized the Java Certification Training Course. After completing the course, he got a 100% hike and desired role! Checkout Simplilearn java full stack review here!

About the Author

Ravikiran A SRavikiran A S

Ravikiran A S works with Simplilearn as a Research Analyst. He an enthusiastic geek always in the hunt to learn the latest technologies. He is proficient with Java Programming Language, Big Data, and powerful Big Data Frameworks like Apache Hadoop and Apache Spark.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.