SoFunction
Updated on 2025-03-03

Java example code for printing asterisk patterns and digital patterns

Print patterns using loop and control statements
In Java, using loops and control statements is the best way to print patterns. Loops can help you repeat a piece of code until a certain condition is met, while control statements allow you to change the process of the program when certain conditions are met. Below are some common Java pattern programs examples.

1. Asterisk pattern

Asterisk patterns are very popular patterns programs in Java and are often used to create interesting visual designs or graphics. These programs use asterisks (*) or other symbols to create various shapes and patterns. Asterisk patterns are commonly used in computer graphics, logo design, and other visual displays.

Creating an asterisk pattern involves using nested loops to control the number of rows, columns, and the position of the asterisk. Programs can be customized to create various patterns including triangles, squares, circles, and more. Here are some common asterisk patterns examples:

1.1 Asterisk Pyramid

public class StarPyramid {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 1; i <= rows; i++) {
            // Print spaces            for (int j = rows - i; j > 0; j--) {
                (" ");
            }
            // Print asterisk            for (int k = 1; k <= (2 * i - 1); k++) {
                ("*");
            }
            ();
        }
    }
}

Output:

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

1.2 Right triangle

public class RightTriangle {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++) {
                ("* ");
            }
            ();
        }
    }
}

Output:


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

1.3 Left triangle

public class LeftTriangle {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 1; i &lt;= rows; i++) {
            // Print spaces            for (int j = rows - i; j &gt; 0; j--) {
                (" ");
            }
            // Print asterisk            for (int k = 1; k &lt;= i; k++) {
                ("* ");
            }
            ();
        }
    }
}

Output:

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

1.4 Rhombus Pattern

public class DiamondPattern {
    public static void main(String[] args) {
        int rows = 5;
        
        // The upper part        for (int i = 1; i &lt;= rows; i++) {
            for (int j = rows - i; j &gt; 0; j--) {
                (" ");
            }
            for (int k = 1; k &lt;= (2 * i - 1); k++) {
                ("*");
            }
            ();
        }
        
        // The lower half        for (int i = rows - 1; i &gt;= 1; i--) {
            for (int j = rows - i; j &gt; 0; j--) {
                (" ");
            }
            for (int k = 1; k &lt;= (2 * i - 1); k++) {
                ("*");
            }
            ();
        }
    }
}

Output:

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

2. Digital patterns

Digital patterns are another common Java patterning program that involves printing numbers in a specific sequence or arrangement. These programs can be used to create visual presentations such as tables, charts, etc.

Creating a digital pattern involves using loops to control the number of rows, columns, and printed numeric values. The program can be customized to create various patterns including multiplication tables, Fibonacci sequences, etc. Here are some common digital patterns examples:

2.1 Digital Pyramid

public class NumberPyramid {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 1; i &lt;= rows; i++) {
            // Print spaces            for (int j = rows - i; j &gt; 0; j--) {
                (" ");
            }
            // Print numbers            for (int k = 1; k &lt;= i; k++) {
                (k + " ");
            }
            ();
        }
    }
}

Output:

    1 
   1 2 
  1 2 3 
 1 2 3 4 
1 2 3 4 5 

2.2 Multiplication table

public class MultiplicationTable {
    public static void main(String[] args) {
        int rows = 10;
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= rows; j++) {
                ("%d * %d = %d\t", i, j, i * j);
            }
            ();
        }
    }
}

Output:

1 * 1 = 1    1 * 2 = 2    1 * 3 = 3    1 * 4 = 4    1 * 5 = 5    1 * 6 = 6    1 * 7 = 7    1 * 8 = 8    1 * 9 = 9    1 * 10 = 10    
2 * 1 = 2    2 * 2 = 4    2 * 3 = 6    2 * 4 = 8    2 * 5 = 10    2 * 6 = 12    2 * 7 = 14    2 * 8 = 16    2 * 9 = 18    2 * 10 = 20    
3 * 1 = 3    3 * 2 = 6    3 * 3 = 9    3 * 4 = 12    3 * 5 = 15    3 * 6 = 18    3 * 7 = 21    3 * 8 = 24    3 * 9 = 27    3 * 10 = 30    
4 * 1 = 4    4 * 2 = 8    4 * 3 = 12    4 * 4 = 16    4 * 5 = 20    4 * 6 = 24    4 * 7 = 28    4 * 8 = 32    4 * 9 = 36    4 * 10 = 40    
5 * 1 = 5    5 * 2 = 10    5 * 3 = 15    5 * 4 = 20    5 * 5 = 25    5 * 6 = 30    5 * 7 = 35    5 * 8 = 40    5 * 9 = 45    5 * 10 = 50    
6 * 1 = 6    6 * 2 = 12    6 * 3 = 18    6 * 4 = 24    6 * 5 = 30    6 * 6 = 36    6 * 7 = 42    6 * 8 = 48    6 * 9 = 54    6 * 10 = 60    
7 * 1 = 7    7 * 2 = 14    7 * 3 = 21    7 * 4 = 28    7 * 5 = 35    7 * 6 = 42    7 * 7 = 49    7 * 8 = 56    7 * 9 = 63    7 * 10 = 70    
8 * 1 = 8    8 * 2 = 16    8 * 3 = 24    8 * 4 = 32    8 * 5 = 40    8 * 6 = 48    8 * 7 = 56    8 * 8 = 64    8 * 9 = 72    8 * 10 = 80    
9 * 1 = 9    9 * 2 = 18    9 * 3 = 27    9 * 4 = 36    9 * 5 = 45    9 * 6 = 54    9 * 7 = 63    9 * 8 = 72    9 * 9 = 81    9 * 10 = 90    
10 * 1 = 10    10 * 2 = 20    10 * 3 = 30    10 * 4 = 40    10 * 5 = 50    10 * 6 = 60    10 * 7 = 70    10 * 8 = 80    10 * 9 = 90    10 * 10 = 100    

2.3 Fibonacci sequence

public class FibonacciPattern {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 1; i <= rows; i++) {
            int a = 0, b = 1;
            for (int j = 1; j <= i; j++) {
                (a + " ");
                int sum = a + b;
                a = b;
                b = sum;
            }
            ();
        }
    }
}

Output:


0 1 
0 1 1 
0 1 1 2 
0 1 1 2 3 

summary

With the above examples, you can see the basic method of printing patterns in Java. Using nested loops and appropriate control statements, various complex patterns can be easily generated. These exercises not only help to understand the loop and control structure, but also improve your programming skills. Hope these examples help you!

The above is the detailed content of the example code for Java printing asterisk patterns and digital patterns. For more information about Java printing asterisks and digital patterns, please pay attention to my other related articles!