-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdiamond.java
More file actions
59 lines (54 loc) · 1.08 KB
/
diamond.java
File metadata and controls
59 lines (54 loc) · 1.08 KB
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
/*
Enter the number of rows: 4
Enter symbol as you wish:
*
*
***
*****
*******
*****
***
*
*/
import java.util.Scanner;
public class diamond {
public static void main(String[] args) {
int i,j;
Scanner scan=new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows=scan.nextInt();
System.out.println("Enter symbol as you wish: ");
char ch=scan.next().charAt(0);
//read number input from user
i=1;
while(i<=rows){//first outer while loop iterates through the row
j=1;
while(j<=rows-i){
System.out.print(" ");//print star
j++;
}
j=1;
while(j<=i*2-1){
System.out.print(ch);//print given symbol
j++;
}
System.out.println();//move to next line
i++;
}
i=rows-1;
while(i>=0){//second outer while loop
j=1;
while(j<=rows-i){
System.out.print(" ");//print space - inner wile loop
j++;
}
j=1;
while(j<=i*2-1){
System.out.print(ch);//print given symbol - inner while loop
j++;
}
System.out.println();
i--;
}
}
}