Today we are you going to learn some coding tricks to show the patterns on console using stars (*). Let jump into the sections.
1. Christmas Tree :
public class ChristmasTree {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Christmas tree drawing program.");
System.out.print("Enter the height: ");
int n = sc.nextInt();
System.out.print("Enter the levels: ");
int level = sc.nextInt();
if (n < 1) {
System.out.println("Height must be positive number.");
} else if (level < 1) {
System.out.println("Number of levels must be positive");
} else {
for (int i = 0; i < level; i++) {
printStars(n);
}
}
sc.close();
}
private static void printStars(int height) {
for (int j = 0; j < height; j++) { // Number of rows!!!
for (int k = j; k < height - 1; k++) { // For printing the spaces!!!
System.out.print(" ");
}
for (int m = 0; m <= (j * 2); m++) { // Printing the stars!!!
System.out.print("*");
}
System.out.println();
}
}
Output: Christmas tree drawing program.
Enter the height: 10
Enter the levels: 3
2. Rectangle :
public static void rectangle(){
int number = 7;
for (int i = 0; i < number; i++) {
if (i == 0 || i == 6) {
for (int j = 0; j < number; j++) {
System.out.print("*");
}
System.out.println();
}
if (i >= 1 && i <= 5) {
for (int j = 0; j < number; j++) {
if (j == 0 || j == 6) {
System.out.print("*");
} else if (j >= 1 && j <= 5) {
System.out.print(" ");
}
}
System.out.println();
}
}
}
3. W- Using *:
public class WPattern{
private static void stars(int count) {
for (int i = 0; i < count; ++i)
System.out.print("*");
}
private static void spaces(int count) {
for (int i = 0; i < count; ++i)
System.out.print(" ");
}
public static void main(String[] args) {
int n = 8;
for (int i = 0; i < n; ++i) {
stars(i + 1);
spaces(n - i - 1);
stars(n - i + 1);
spaces(2 * i);
stars(n - i);
spaces(n - i - 1);
stars(i + 1);
System.out.println();
}
}
}
public class ChristmasTree {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Christmas tree drawing program.");
System.out.print("Enter the height: ");
int n = sc.nextInt();
System.out.print("Enter the levels: ");
int level = sc.nextInt();
if (n < 1) {
System.out.println("Height must be positive number.");
} else if (level < 1) {
System.out.println("Number of levels must be positive");
} else {
for (int i = 0; i < level; i++) {
printStars(n);
}
}
sc.close();
}
private static void printStars(int height) {
for (int j = 0; j < height; j++) { // Number of rows!!!
for (int k = j; k < height - 1; k++) { // For printing the spaces!!!
System.out.print(" ");
}
for (int m = 0; m <= (j * 2); m++) { // Printing the stars!!!
System.out.print("*");
}
System.out.println();
}
}
Output: Christmas tree drawing program.
Enter the height: 10
Enter the levels: 3
2. Rectangle :
public static void rectangle(){
int number = 7;
for (int i = 0; i < number; i++) {
if (i == 0 || i == 6) {
for (int j = 0; j < number; j++) {
System.out.print("*");
}
System.out.println();
}
if (i >= 1 && i <= 5) {
for (int j = 0; j < number; j++) {
if (j == 0 || j == 6) {
System.out.print("*");
} else if (j >= 1 && j <= 5) {
System.out.print(" ");
}
}
System.out.println();
}
}
}
3. W- Using *:
public class WPattern{
private static void stars(int count) {
for (int i = 0; i < count; ++i)
System.out.print("*");
}
private static void spaces(int count) {
for (int i = 0; i < count; ++i)
System.out.print(" ");
}
public static void main(String[] args) {
int n = 8;
for (int i = 0; i < n; ++i) {
stars(i + 1);
spaces(n - i - 1);
stars(n - i + 1);
spaces(2 * i);
stars(n - i);
spaces(n - i - 1);
stars(i + 1);
System.out.println();
}
}
}
Comments
Post a Comment