-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattern-5-sol2.java
34 lines (25 loc) · 985 Bytes
/
pattern-5-sol2.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
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt(); // n should be odd always
// initial values of space and star
int space = n/2;
int star = 1;
for(int row=1; row<=n; row++){
// printing space first
for(int j=1; j<=space; j++)
System.out.print("\t");
// printing starts now
for(int k=1; k<=star; k++)
System.out.print("*\t");
// checking whether space and star have to be incremented or decremented
if(row<=n/2){ // for row above and equal to n/2
space--; star += 2;
}else{ // for row below n/2
space++; star -= 2;
}
System.out.println(); // for next line
}
}
}