-
Notifications
You must be signed in to change notification settings - Fork 139
/
Patterns.java
83 lines (75 loc) · 1.95 KB
/
Patterns.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
import org.w3c.dom.ls.LSOutput;
public class Patterns {
public static void main(String[] args) {
Pattern1(5);
System.out.println();
Pattern2(5);
System.out.println();
Pattern3(5,0);
System.out.println();
Pattern4(5);
System.out.println();
Pattern5(5);
System.out.println();
pattern6(5);
}
static void Pattern1(int n){
for (int i = 1; i <=n ; i++) {
for (int j = 1; j <=i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
static void Pattern2(int n){
for (int i = 1; i <=n ; i++) {
for (int j = 1; j <=n ; j++) {
System.out.print("* ");
}
System.out.println();
}
}
static void Pattern3(int r,int c){
if (r==0){
return;
}
if(c < r){
System.out.print("* ");
Pattern3(r,c+1);
}else{
System.out.println();
Pattern3(r-1,0);
}
}
static void Pattern4(int n){
for (int i = 1; i <=n ; i++) {
for (int j = 1; j <=i ; j++) {
System.out.print(j);
}
System.out.println();
}
}
static void Pattern5(int n){
for (int i = 0; i <=2*n; i++) {
int totalcols = i > n ? 2*n-i:i;
for (int j = 1; j <totalcols ; j++) {
System.out.print("* ");
}
System.out.println();
}
}
static void pattern6(int n){
for (int i = 1; i <=n; i++) {
for (int k = 0; k <n-i ; k++) {
System.out.print(" ");
}
for (int j = i; j >=1 ; j--) {
System.out.print(j+" ");
}
for (int j = 2; j <=i; j++) {
System.out.print(j+" ");
}
System.out.println();
}
}
}