-
Notifications
You must be signed in to change notification settings - Fork 0
/
Diamond.java
53 lines (46 loc) · 1.08 KB
/
Diamond.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
import java.io.*;
import java.util.*;
// Name: Diamond pattern filled with numbers
public class Diamond {
public static void main(String []args){
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for(int i =0; i<n; i++)
{
int start= i+1;
int j =0;
int num = Math.abs(n/2 -i);
int count;
if(n%2==0)
{
if(i<n/2)
{ num--;}
}
while(j<num)
{
System.out.print(" ");
j++;
}
j=0;
count= n-(2*num);
if(n%2==0)
{
count--;
}
j=0;
while(j<count)
{
if(j<count/2)
{
System.out.print(start++);
}
else
{
System.out.print(start--);
}
j++;
}
System.out.println("");
}
}
}