Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

BW

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

code:

import java.util.Scanner;

public class hllwdmnd {


public static void main(String[] args) {
int i, j, n;
Scanner sc = new Scanner(System.in);
System.out.print("enter an odd number:");
n = sc.nextInt();
while (n%2==0) {
System.out.print("please enter an odd number:");
n = sc.nextInt();
}
for (i = 1; i <= n; i++) {
for (j = 1; j < (n - i + 1); j++)
System.out.print(" ");
for (j = 1; j <= ((i * 2) - 1); j++) {
if (((j == (i * 2) - 1) || j == 1) || ((i > ((n / 2)+1)) && ((j ==
2*(i-(n/2)-0.5))||(j==((i * 2)-2*(i-(n/2)-0.5))))))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println("");
}
for (i = (n-1); i >= 1; i--) {
for (j = 1; j < (n - i + 1); j++)
System.out.print(" ");
for (j = 1; j <= ((i * 2) - 1); j++) {
if (((j == (i * 2) - 1) || j == 1) || ((i > ((n / 2)+1)) && ((j ==
2*(i-(n/2)-0.5))||(j==((i * 2)-2*(i-(n/2)-0.5))))))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println("");
}
}
}
output:
enter an odd number:23
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*

You might also like