-
Notifications
You must be signed in to change notification settings - Fork 0
/
patterns.java
141 lines (138 loc) · 3.92 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package javaPractice;
import java.util.Scanner;
public class patterns{
public 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();
}
}
public static void pattern2(int n){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= i; j++){
if((i+j)%2==0){
System.out.print("1" + " ");
}else{
System.out.print("0" + " ");
}
}
System.out.println();
}
}
public static void space(int n){
for(int i = 0; i < n; i++){
System.out.println("----------------");
}
}
public static void revPattern(int n){
for(int i = 0; i < n; i++){
for(int j = 0; j < n-i; j++){
System.out.print(" ");
}
for(int j = 0; j < i; j++){
System.out.print("*");
}
System.out.println();
}
}
public static void pattern3(int n){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n - i + 1; j++){
System.out.print(j + " ");
}
System.out.println();
}
}
public static void floydPattern(int n){
int dig = 1;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= i; j++){
System.out.print(dig + " ");
dig++;
}
System.out.println();
}
}
public static void pattern4(int n){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n-i; j++){
System.out.print(" ");
}
for(int j = 1; j<= n; j++){
System.out.print("*");
}
System.out.println();
}
}
public static void pattern5(int n){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(i == 1 || i == n || j == 1 || j == n){
System.out.print("*" );
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
public static void pattern6(int n){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n - i; j++){
System.out.print(" ");
}
for(int j = 1; j <= n; j++){
if(i == 1 || i == n || j == 1 || j == n){
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
public static void pattern7(int n){
for(int i = 0; i < n; i++){
for(int j = 0; j < n-i; j++){
System.out.print(" ");
}
for(int j = 0; j < (2*i) + 1; j++){
System.out.print("*");
}
System.out.println();
}
for(int i = n - 1; i >= 0; i--){
for(int j = 0; j < n-i; j++){
System.out.print(" ");
}
for(int j = 0; j < (2*i) + 1; j++){
System.out.print("*");
}
System.out.println();
}
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number");
int n = sc.nextInt();
pattern1(n);
space(2);
pattern2(n);
space(2);
revPattern(n);
space(2);
pattern3(n);
space(2);
floydPattern(n);
space(2);
pattern4(n);
space(3);
pattern5(n);
space(2);
pattern6(n);
space(2);
pattern7(n);
}
}