0% found this document useful (0 votes)
24 views2 pages

BW

The document contains Java code that takes in an odd integer from the user and prints out a diamond shape pattern of asterisks of that size. It first checks that the input is odd, then uses nested for loops to print spaces and asterisks in the correct positions to create the shape with asterisks at the edges and center decreasing to a single asterisk at the middle and then increasing back out.

Uploaded by

shanmukn21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views2 pages

BW

The document contains Java code that takes in an odd integer from the user and prints out a diamond shape pattern of asterisks of that size. It first checks that the input is odd, then uses nested for loops to print spaces and asterisks in the correct positions to create the shape with asterisks at the edges and center decreasing to a single asterisk at the middle and then increasing back out.

Uploaded by

shanmukn21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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