-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfish-pattern
67 lines (54 loc) · 2.09 KB
/
fish-pattern
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
// import required classes
import java.util.Scanner;
// create FishPatternExample class to print Fish Pattern
public class FishPatternExample {
// create drawFistPattern() method that will print Fish over n rows
public static void drawFistPattern(int n) {
// declare and initialize string variables that help to print Fish pattern
String sp1 = "";
String st1 = "*", st2 = "";
// for generating space string
for (int i = 0; i < n; ++i) {
sp1 += ' ';
}
// for printing upper, middle and lower part
for (int j = 0; j < 2 * n + 1; ++j)
{
// for printing upper part
if (j < n) {
System.out.print(sp1 + st1 + sp1 + sp1);
System.out.println(st2);
sp1 = sp1.substring(0, sp1.length() - 1);
st1 += "**";
st2 += "*";
}
// for printing middle part
if (j == n) {
System.out.print(sp1 + st1 + sp1 + sp1);
System.out.println(st2);
}
// for printing lower part
if (j > n) {
sp1 += ' ';
st1 = st1.substring(0, st1.length() - 1);
st1 = st1.substring(0, st1.length() - 1);
st2 = st2.substring(0, st2.length() - 1);
System.out.print(sp1 + st1 + sp1 + sp1);
System.out.println(st2);
}
}
}
// main() method start
public static void main(String args[]) {
int n;
// create Scanner class object
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of N");
// take input from user
n = sc.nextInt();
// close Scanner class object
sc.close();
// call drawFistPattern() method for printing Fish Pattern
drawFistPattern(n);
}
}