Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

457
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!