Java program to create pyramid and pattern

www‮figi.‬tidea.com

Here are some examples of Java programs to create pyramid and pattern:

  1. Pyramid of stars
public class PyramidExample {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < rows - i; j++) {
                System.out.print(" ");
            }
            for (int k = 0; k <= i; k++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

Output:

* 
    * * 
   * * * 
  * * * * 
 * * * * *
  1. Pyramid of numbers
public class NumberPyramidExample {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= rows - i; j++) {
                System.out.print(" ");
            }
            for (int k = 1; k <= i; k++) {
                System.out.print(k + " ");
            }
            System.out.println();
        }
    }
}

Output:

1 
   1 2 
  1 2 3 
 1 2 3 4 
1 2 3 4 5
  1. Pattern of stars
public class PatternExample {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

Output:

* 
* * 
* * * 
* * * * 
* * * * *
  1. Pattern of numbers
public class NumberPatternExample {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }
}

Output:

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

These are just a few examples of the types of patterns that can be created with Java programs. You can modify the code to create different types of patterns by changing the loop conditions and the characters that are printed to the console.