In: Computer Science
Using the IntelliJ IDEA tool, write a program that will print a Christmas tree with the "*". The tree should have at least 10 lines. Look back at the birthday cake lab for examples of how to do this lab. Submit a screen shot of your output in the console (i.e. non REPL tool).
Answer = We need to print Christmas Tree like pattern. The source code provided below is in Java language. Comments are shown by "//" operator at the starting of line.
Source Code :
// import scanner to take input from user
import java.util.Scanner;
public class ChristmasTree {
static void showChristmasTree(int num) {
// start loop to print Christmas Tree
for (int i = 0; i < num; i++) {
for (int j = 0; j < 2 * num; j++) {
if ((num - 1 - i) <= j && j <= (num - 1 + i)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.print("\n");
}
// End loop
}
// Main Class
public static void main(String[] args) {
System.out.println("Enter number of rows to print Christmas Tree Pattern");
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
// Call function to print Christmas tree
showChristmasTree(num);
scan.close();
}
}
Output : Save the file with file name "ChristmasTree.java" and follow below commands to see output.
command (i) = javac ChristmasTree.java
command (ii) = java ChristmasTree
The screenshot of output is:
Here program ask user to give input of total rows, then display tree.