How to make a diamond using nested for loops

88,746

Solution 1

In order to make a diamond you need to set spaces and stars in shape. I have made this simple program using only nested loops since I am a beginner.

public class Diamond {
    public static void main(String[] args) {
        int size = 9,odd = 1, nos = size/2; // nos =number of spaces
        for (int i = 1; i <= size; i++) { // for number of rows i.e n rows
            for (int k = nos; k >= 1; k--) { // for number of spaces i.e
                                                // 3,2,1,0,1,2,3 and so on
                System.out.print(" ");
            }
            for (int j = 1; j <= odd; j++) { // for number of columns i.e
                                                // 1,3,5,7,5,3,1
                System.out.print("*");
            }
            System.out.println();
            if (i < size/2+1) {
                odd += 2; // columns increasing till center row 
                nos -= 1; // spaces decreasing till center row 
            } else {
                odd -= 2; // columns decreasing
                nos += 1; // spaces increasing

            }
        }
    }
}

As you can see nos is the number of spaces. It needs to be decreased until the center row, and the number of stars needs to be increased but after the center row it's the opposite, i.e spaces increase and stars decrease.

size can be any number. I set it to 9 over here so I will have a size 9 star that is 9 rows and 9 columns max... number of space (nos) will be 9/2 = 4.5 . But java will take it as 4 because int can not store decimal numbers and the center row will be 9/2 + 1 = 5.5, which will result in 5 as int.

So first you will make rows... 9 rows hence

(int i=1;i<=size;i++) //size=9

then print spaces like I did

(int k =nos; k>=1; k--) //nos being size/2

then finally stars

(int j=1; j<= odd;j++)

once the line ends...

You can adjust stars and spaces using an if condition.

Solution 2

for (int i = 0; i < 5; i++)
    System.out.println("    *********".substring(i, 5 + 2 * i));

for (int i = 5; i > 0; i--)
    System.out.println("     **********".substring(i - 1, 5 + (2 * i) - 3));

Solution 3

public class MyDiamond {
    public static void main(String[] args) {
        //Length of the pyramid that we want.151 is just an example
        int numRows = 151;
        //midrow is the middle row and has numRows number of *
        int midrow = (numRows + 1) / 2;

        int diff = 0;
        for (int i = 1; i < numRows + 1; i++) {
            for (int j = 1; j < numRows + 1; j++) {
                if (((midrow - diff) <= j && (j <= midrow + diff))) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
            if (i < midrow) {
                diff++;
            } else {
                diff--;
            }
        }
    }
}

Solution 4

enter image description here

Note: Using Count Global variable we can manage space as well as star increment and decrement.

import java.util.*;
public class ParamidExample {
    public static void main(String args[]) {
        System.out.println("Enter a number");
        Scanner sc = new Scanner(System.in);
        int no = sc.nextInt();

        int count = 1;
        for (int i = 1; i <= 2 * no - 1; i++) {
            for (int j = count; j <= no; j++) {
                System.out.print("  ");
            }
            for (int k = 1; k <= count * 2 - 1; k++) {
                System.out.print("* ");
            }
            if (i < no)
                count++;
            else
                count--;
            System.out.println("");
        }
    }
}

Solution 5

public class Diamond {
    //Size of the diamond
    private int diagonal;

    public Diamond(int diagonal) {
        this.diagonal = diagonal;
    }

    public void drawDiamond() {
        int n = diagonal;
        for (int i = n / 2; i >= -n / 2; i--) {
            for (int k = 0; k < i; k++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= (n - i * 2) && i >= 0; j++) {
                System.out.print("*");
            }
            for (int k = 1; k <= -i && i < 0; k++) {
                System.out.print(" ");
            }
            for (int j = (n / 2) * 2 + 2 * i; j >= -(n % 2 - 1) && i < 0; j--) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        //You pass diamond size here in the constructor
        Diamond a = new Diamond(21);
        a.drawDiamond();
    }
}

The main problem is parity of diagonal. If it's even you can't properly draw top asterisk. So there is 2 types of diamonds - with even and odd diagonal (with 2 and 1 asterisk at the top).

Share:
88,746
Anshul Desai
Author by

Anshul Desai

Updated on January 13, 2022

Comments

  • Anshul Desai
    Anshul Desai about 2 years

    So I was assigned to make a diamond with asterisks in Java and I'm really stumped. Here's what I've come up with so far:

    public class Lab1 {
        public static void main(String[] args) {
            for (int i = 5; i > -5; i--) {
                for (int j = 0; j < i; j++) {
                    System.out.print(" ");
                }
                for (int j = 0; j >= i; j--) {
                    System.out.print(" ");
                }
                System.out.println("*");
            }
        }
    }
    

    output