Java Patterns

 
 
In this article, I am giving some examples to create different pyramid patterns from numbers, symbols and star. We will also look at some examples of creating an inverted pyramid pattern in Java program.
 
Pattern Example 1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public class Pattern {
	public static void main(String[] args) {
		for (int i = 0; i < 5; i++) {
			int x = 1;
			for (int j = 0; j <= i; j++) {
				System.out.print(x+" ");
				x++;
			}
			System.out.println();
		}
	}
}

Output

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


Pattern Example 2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public class Pattern {
	public static void main(String[] args) {
		for (int i = 0; i < 10; i++) {
			for (int j = 0; j <=i; j++) {
				System.out.print(i+" ");
			}
			System.out.println();
		}
	}
}

0 
1 1 
2 2 2 
3 3 3 3 
4 4 4 4 4 
5 5 5 5 5 5 
6 6 6 6 6 6 6 
7 7 7 7 7 7 7 7 
8 8 8 8 8 8 8 8 8 
9 9 9 9 9 9 9 9 9 9 

Pattern Example 3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import java.util.Scanner;
public class Pattern {
	public static void print(String s, int n) {
		for (int j = 0; j < n; j++) {
			System.out.print(s);
		}
	}
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("Please enter the length to print:");
		int n = input.nextInt();	
		for (int i = 1; i <= n; i++) {
			int whiteSpaces = n - i;
			print(" ", whiteSpaces);
			print(i + " ", i);
			System.out.println("");
		}
		input.close();
	}
}

Please enter the length to print:
9
        1 
       2 2 
      3 3 3 
     4 4 4 4 
    5 5 5 5 5 
   6 6 6 6 6 6 
  7 7 7 7 7 7 7 
 8 8 8 8 8 8 8 8 
9 9 9 9 9 9 9 9 9 

Pattern Example 4 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.Scanner;
public class Pattern {
	public static void printWhiteSpaces(String s, int n) {
		for (int j = 0; j < n; j++) {
			System.out.print(s);
		}
	}	
	public static void printNumber(int n) {
		for (int k = 1; k <= n; k++) {
			System.out.print(k+ " ");
		}
	}
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("Please enter the length to print:");
		int n = input.nextInt();	
		for (int i = 1; i <= n; i++) {
			int whiteSpaces = n - i;
			printWhiteSpaces(" ", whiteSpaces);
			printNumber(i);
			System.out.println("");
		}
		input.close();
	}
}

Please enter the length to print:
9
        1 
       1 2 
      1 2 3 
     1 2 3 4 
    1 2 3 4 5 
   1 2 3 4 5 6 
  1 2 3 4 5 6 7 
 1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8 9 

Pattern Example 5

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import java.util.Scanner;

public class Pattern {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("Please enter the length to print:");
		int n = input.nextInt();
		int x = 1;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j <= i; j++) {
				System.out.print(x + " ");
				x++;
			}
			System.out.println("");
		}
		input.close();
	}
}

Please enter the length to print:
8
1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 21 
22 23 24 25 26 27 28 
29 30 31 32 33 34 35 36 

Pattern Example 6

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.Scanner;

public class Pattern {
	public static void printWhiteSpaces(String s, int n) {
		for (int j = 0; j < n; j++) {
			System.out.print(s);
		}
	}
	public static void generate(int number) {
		int tri[][] = new int[number][];
		for (int i = 0; i < number; i++) {
			tri[i] = new int[i + 1];
			int whiteSpaces = number - i;
			printWhiteSpaces(" ", whiteSpaces);
			for (int j = 0; j <= i; j++) {
				if (i == 0 || j == 0 || j == i) {
					tri[i][j] = 1;
				} else {
					tri[i][j] = tri[i - 1][j] + tri[i - 1][j - 1];
				}
				System.out.print(tri[i][j] + " ");
			}
			System.out.println("");
		}
	}
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("Please enter the size of the Pascal's Triangle you want:");
		int n = input.nextInt();
		generate(n);
	}
}
Please enter the size of the Pascal's Triangle you want:
9
         1 
        1 1 
       1 2 1 
      1 3 3 1 
     1 4 6 4 1 
    1 5 10 10 5 1 
   1 6 15 20 15 6 1 
  1 7 21 35 35 21 7 1 
 1 8 28 56 70 56 28 8 1 

Next Post Previous Post
No Comment
Add Comment
comment url