Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

461
Vistas
How to print bowtie in java?

How to make this pattern if input N = 5 Output :

*    *
**  **
******
**  **
*    *

Here's my code

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

The result is like this:

*    *
**  **
******
******
******
******

How could I fix this?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You have used OR || to combine the upper down-pointing triangle of " " and the lower up-pointing triangle of " ". Good idea.

You describe the upper triangle as (j > i && j < n - i), that is correct.

However, you describe the lower triangle as (j > (n-i) && j < i && i > n).
That is where the problem is. The i > n is never true for i from 0 to n as in your outer loop construct. In order to describe the lower half of the pattern, you should have used i>n/2. You could have however dropped that part of the condition altogether, because the counter comparisons in the first part would only apply in the lower half anyway.
So (j > (n-i) && j < i).

That gets you:

*    *
**  **
******
******
**  **

By the way, n lines, not n+1 lines in the outer loop.

As you can see, the lower triangle of " " is much too small. Both, to the right side and to the left side.
So you need to make your conditions more generous, i.e. "lower-equal" and "higher-equal" instead of "lower" and "higher". In code (j >= (n-i) && j <= i).

Putting it together:

public class Main {
    static final int n = 5;

    public static void main(String[] args)
    {
        for(int i = 0; i < n; i++) // only n lines
        {
            for(int j = 0; j < n+1; j++) {
                if ((j > i && j < n - i) ||
                    (j >= (n-i) && j <= i)) // more generous and no second condition
                    System.out.print(" ");
                else
                    System.out.print("*");
            }
            System.out.println();
        }
    }
}

This code is very similar to your attempt and gets you the desired

*    *
**  **
******
**  **
*    *
over 4 years ago · Santiago Trujillo Denunciar

0

Your existing attempt is actually really close here is a working version which fixes the second condition (that in your current attempt would always be false):

class Main {
    public static void main(String[] args) {
        printBowtie(5);
    }

    private static void printBowtie(int n) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n + 1; j++) {
                if ((j > i && j < n - i) || (j >= n - i && j <= i)) {
                    System.out.print(" ");
                } else {
                    System.out.print("*");
                }
            }
            System.out.println();
        }
    }
}

Output:

*    *
**  **
******
**  **
*    *

Relevant diff:

     private static void printBowtie(int n) {
-        for (int i = 0; i < n + 1; i++) {
+        for (int i = 0; i < n; i++) {
             for (int j = 0; j < n + 1; j++) {
-                if ((j > i && j < n - i) || (j > (n - i) && j < i && i > n)) {
+                if ((j > i && j < n - i) || (j >= n - i && j <= i)) {
over 4 years ago · Santiago Trujillo Denunciar

0

You can simplify it greatly by putting the common code in a method as shown below:

public class Main {
    static final int LINES = 5;

    public static void main(String[] args) {
        // Upper half
        for (int line = 1; line <= LINES / 2 + 1; line++) {
            print(line);
        }

        // Lower half
        for (int line = LINES / 2; line >= 1; line--) {
            print(line);
        }
    }

    static void print(int line) {
        for (int j = 1; j <= line; j++) {
            System.out.print("*");
        }
        for (int j = 1; j <= LINES - 2 * line + 1; j++) {
            System.out.print(" ");
        }
        for (int j = 1; j <= line; j++) {
            System.out.print("*");
        }
        System.out.println();
    }
}

Output:

*    *
**  **
******
**  **
*    *
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda