Abhishek Dutta
Abhishek Dutta

Reputation: 31

Print an hourglass pattern

I want to print the pattern in java and I have tried the following code. please guide me to solve my mistake.

import java.util.*;

class sp10
{
    public static void main(String args[])
    { 
        int i,j,n;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter value of n");
        n = sc.nextInt();

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

I am trying it in Java. Can you please guide me? It should print 5 stars in the first and the last line. Given below is the exact pattern

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

Upvotes: 2

Views: 319

Answers (3)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79620

Probably, what you intended to do is as follows:

import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        int i, j, n;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter value of n: ");
        n = sc.nextInt();

        for (i = n; i >= 1; i--) {
            for (j = i; j < n; j++) {
                System.out.print(" ");
            }

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

A sample run:

Enter value of n: 4
* * * *
 *   *
  * *
   *
  * *
 *   *
* * * *

However, if you insist that you need to print exactly what you have shown in the question, you can do it as follows:

import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        int i, j, n;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter value of n: ");
        n = sc.nextInt();

        for (i = n; i >= 1; i--) {
            for (j = i; j < n; j++) {
                System.out.print(" ");
            }

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

Output:

Enter value of n: 4
 ***** 
 *   *
  * *
   *
  * *
 *   *
 ***** 

Upvotes: 0

ntalbs
ntalbs

Reputation: 29468

Well, let's look at the pattern.

n = 5
*****
*   *
 * *
  *
 * *
*   *
***** 

The first line and the last line is just print * n times.

private static void printTop(int n) {
    System.out.println("*".repeat(n));
}

private static void printBottom(int n) {
    printTop(n);
}

Though I wrote two methods here, you can just define one method like printStar(int n) and reuse it on the first and last line.

The real problem is to implement the middle part. Well, this still can be implemented in a relatively simple way. You just need to move * from column 0 to right and from n-1 to left as the row increased.

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

p is the offset from column 0 to right or column n-1 to left.

Now, you have everything. You can write the main method like the following:

public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter value of n");
    int n = sc.nextInt();

    printTop(n);
    printMiddle(n);
    printBottom(n);
}

Upvotes: 1

Andrea Annibali
Andrea Annibali

Reputation: 389

I suggest the following solution:

import java.util.*;

public class sp10 {

    public static void main(String args[]) {
        int i, j, n;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter value of n");
        n = sc.nextInt();

        System.out.print(" ");
        for (i = 0; i < 2*n - 3; i++) {
            System.out.print("*");
        }
        System.out.println("");
        for (i = n-1; i >= 1; i--) {
            for (j = i; j <n; j++) {
                System.out.print(" ");
            }

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

I hope is what you're looking for, because you question was a little bit difficult to understand...

With such solution the output is (for n = 4):

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

Upvotes: 0

Related Questions