-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattern.java
102 lines (92 loc) · 2.98 KB
/
pattern.java
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import java.util.Scanner;
public class pattern{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Menu to ask which pattern to print
System.out.println("Enter a number (1-5) to print the corresponding pattern:");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("Enter the number of rows:");
int rows = scanner.nextInt();
pattern1(rows); // Pattern 1 now accepts a row count.
break;
case 2:
pattern2();
break;
case 3:
pattern3();
break;
case 4:
pattern4();
break;
case 5:
pattern5();
break;
default:
System.out.println("Invalid choice. Please enter a number between 1 and 5.");
break;
}
scanner.close();
}
// Function for pattern 1 - Prints given number of rows with 8 *
public static void pattern1(int rows) {
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= 8; j++) {
System.out.print("* ");
}
System.out.println();
}
}
// Function for pattern 2 - Left-aligned increasing pyramid
public static void pattern2() {
int height = 5; // Set the height of the pyramid
for (int i = 1; i <= height; i++) {
// Print stars for the current row
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println(); // Move to the next line
}
}
// Function for pattern 3 - Right-aligned triangle
public static void pattern3() {
for (int i = 1; i <= 5; i++) {
// Print leading spaces for alignment
for (int j = 5; j > i; j--) {
System.out.print(" ");
}
// Print stars after spaces
for (int k = 1; k <= i; k++) {
System.out.print("* ");
}
System.out.println();
}
}
// Function for pattern 4 - Pyramid shape
public static void pattern4() {
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--) {
System.out.print(" ");
}
for (int k = 1; k <= (2 * i - 1); k++) {
System.out.print("* ");
}
System.out.println();
}
}
// Function for pattern 5 - Proper Pyramid of numbers
public static void pattern5() {
for (int i = 1; i <= 5; i++) {
// Print leading spaces for pyramid alignment
for (int j = 5; j > i; j--) {
System.out.print(" ");
}
// Print the numbers, 'i' printed 'i' times
for (int k = 1; k <= i; k++) {
System.out.print(i + " ");
}
System.out.println();
}
}
}