2

I need to draw a numeric diamond, for example with a height of 9:

    1
   222
  33333
 4444444
555555555
 4444444
  33333
   222
    1

I wrote the code and I managed to get the same diamond, but with stars. I want it with these numbers. How can I do that? Here is what I have done so far:

for (int i = 1; i < 10; i += 2) {
    for (int j = 0; j < 9 - i / 2; j++)
        System.out.print(" ");
    for (int j = 0; j < i; j++)
        System.out.print("a");
    System.out.print("\n");
}

for (int i = 7; i > 0; i -= 2) {
    for (int j = 0; j < 9 - i / 2; j++)
        System.out.print(" ");
    for (int j = 0; j < i; j++)
        System.out.print("b");
    System.out.print("\n");
}
davido
  • 37
  • 5
  • 2
    http://codegolf.stackexchange.com/questions/8696/print-this-diamond -- not what you want, but some interesting approaches ... – Eric Wilson Nov 06 '15 at 14:46

4 Answers4

2

Regarding your code:

  • System.out.print("\n"); should be replaced by System.out.println().
  • You should a dynamic height instead of hard-coding 9.
  • It prints the correct pattern, only what is printed is wrong: instead of printing "a" and "b", you should print the index of the loop and see what you can get from there. This is @Tsung-Ting Kuo solution.

You can do it with fewer loops and a more understandable in my view. Consider the following algorithm:

  • For each row of the pattern (so the row goes from 0 to height excluded)
  • For each column of the pattern (so the column goes from 0 to height excluded)
  • We need to print a space when we are in the upper-right, upper-left, lower-right or lower-left of the diagram.
    • Upper-left: This is when the column is less than height/2-row-1
    • Lower-left: This is when the column is less than row-height/2
      • Factoring these two expressions in a single one, this is when the column is less than height/2 - min where min = Math.min(row+1, height-row).
    • Upper-right: This is when the column is greater than height/2+row+1
    • Lower-right: This is when the column is greater than height/2+height-row
      • Factoring these two expressions in a single one, this is when the column is greater than height/2 + min where min = Math.min(row+1, height-row).
  • Otherwise, we need to print Math.min(row+1, height-row).

Turning into code:

public static void main(String[] args) {
    int height = 9;
    for (int row = 0; row < height; row++) {
        for (int column = 0; column < height; column++) {
            int min = Math.min(row+1, height-row);
            if (column <= height / 2 - min || column >= height / 2 + min) {
                System.out.print(" ");
            } else {
                System.out.print(min);
            }
        }
        System.out.println();
    }
}

Sample output:

    1    
   222   
  33333  
 4444444 
555555555
 4444444 
  33333  
   222   
    1    
Tunaki
  • 132,869
  • 46
  • 340
  • 423
1

java-11

Using String#repeat introduced as part of Java-11, you can do it using a single loop.

public class Main {
    public static void main(String[] args) {
        final int MID_ROW_NUM = 5;
        for (int i = 1 - MID_ROW_NUM; i < MID_ROW_NUM; i++) {
            int x = Math.abs(i);
            System.out.println(" ".repeat(x) + String.valueOf(MID_ROW_NUM - x).repeat((MID_ROW_NUM - x) * 2 - 1));
        }
    }
}

Output:

    1
   222
  33333
 4444444
555555555
 4444444
  33333
   222
    1

You can print a variant of the diamond simply by increasing the amount of space by one character:

public class Main {
    public static void main(String[] args) {
        final int MID_ROW_NUM = 5;
        for (int i = 1 - MID_ROW_NUM; i < MID_ROW_NUM; i++) {
            int x = Math.abs(i);
            System.out.println("  ".repeat(x) + ((MID_ROW_NUM - x) + " ").repeat((MID_ROW_NUM - x) * 2 - 1));
        }
    }
}

Output:

        1 
      2 2 2 
    3 3 3 3 3 
  4 4 4 4 4 4 4 
5 5 5 5 5 5 5 5 5 
  4 4 4 4 4 4 4 
    3 3 3 3 3 
      2 2 2 
        1 
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

Please try the following code (I have tested it), only change two print:

public static void main(String[] args) throws Exception {
    for (int i = 1; i < 10; i += 2) {
        for (int j = 0; j < 9 - i / 2; j++)
            System.out.print(" ");

        for (int j = 0; j < i; j++)
            System.out.print(i/2+1); // Change here

        System.out.print("\n");
    }

    for (int i = 7; i > 0; i -= 2) {
        for (int j = 0; j < 9 - i / 2; j++)
            System.out.print(" ");

        for (int j = 0; j < i; j++)
            System.out.print(i/2+1); // Change here

        System.out.print("\n");

    }
}

Output:

     1
    222
   33333
  4444444
 555555555
  4444444
   33333
    222
     1
Tsung-Ting Kuo
  • 1,171
  • 6
  • 16
  • 21
0

You can draw a numeric rhombus using IntStream as follows:

int m = 5;
int n = 5;
String[][] arr = IntStream
        .rangeClosed(-m, m)
        .map(Math::abs)
        .mapToObj(i -> IntStream
                .rangeClosed(-n, n)
                .map(Math::abs)
                .mapToObj(j -> i + j > Math.max(m, n) ? " " : "" + (m - i))
                .toArray(String[]::new))
        .toArray(String[][]::new);
// formatted output
Arrays.stream(arr).map(row -> String.join(" ", row)).forEach(System.out::println);
          0          
        1 1 1        
      2 2 2 2 2      
    3 3 3 3 3 3 3    
  4 4 4 4 4 4 4 4 4  
5 5 5 5 5 5 5 5 5 5 5
  4 4 4 4 4 4 4 4 4  
    3 3 3 3 3 3 3    
      2 2 2 2 2      
        1 1 1        
          0          

See also: Filling a 2d array with numbers in a rhombus formPrint this diamond