Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /*
- ======================================
- chapter 4: Loops
- Ex15: Christmas tree
- ======================================
- */
- public class MyProgram {
- public static void main(String[] args) {
- //variables
- int base;
- Scanner s=new Scanner(System.in);
- //user input- bulletproof against stupid users
- do{
- System.out.println("Enter odd number for base: ");
- base=s.nextInt();
- }while(base%2==0);
- //printing triangle base in bottom
- for(int row=0;row<base;row++) {
- for (int i = 0; i < base; i++) {
- for (int space = i + 1; space < base; space++) //loop for space
- System.out.print(" ");
- for (int j = base - i - 1; j < base; j++)
- System.out.print("* "); //loop for stars
- System.out.println();
- }
- }
- //loop for tree trunk
- for(int trunk=0;trunk<base;trunk++) {
- for(int space=0; space<base/2;space++) //loop for space
- System.out.print(" ");
- System.out.println("*");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment