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?
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
* *
** **
******
** **
* *
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)) {
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:
* *
** **
******
** **
* *