vendredi 19 mars 2021

How to print given rhombus in attached image with same logic in java or any other language

I have enter image description here tried but not getting close, unable to get the relation between rows and columns to run the loop.`

static void makeRhombus(int rows) {

        int i, j;

       
        int horizontal = 2 * rows;
        int vertical = rows + 1;
        for (i = 1; i <= vertical; i++) {
            /* Print trailing spaces */
            for (j = 1; j <= rows + 1 - i; j++) {
                System.out.print(" ");
            }

            /* Print stars and center spaces */
            for (j = 1; j <= 2 * rows; j++) {
                    if (i != 1 && j == 1 || j == horizontal && i != 1) {
                        System.out.print("/");
                    } else if (i == vertical || i == 1) {
                        System.out.print("_");
                        
                    } else if (i == vertical && j == 1) {
                        System.out.print("/");
                    } else {
                        System.out.print(" ");
                    }
            }
            System.out.print("\n");
        }
    }

Aucun commentaire:

Enregistrer un commentaire