Untitled

 avatar
unknown
java
a year ago
2.0 kB
165
Indexable
import java.util.Scanner;

// int total_number_of_lines = n;

// int stars = 1;
// int spaces = n-1;

// int current_number_of_line = 1;

// while(current_number_of_line <= total_number_of_lines){
//     // print stars
//     for(int i=1; i<=stars; i++){
//         System.out.print("* ");
//     }

//     // print spaces
//     for(int i=1; i<=spaces; i++){
//         System.out.print("  ");
//     }

//     // prepare for next line
//     System.out.println();
//     spaces--;
//     stars++;
//     current_number_of_line++;
// }



// int total_number_of_lines = n;

// // first line values 
// int stars = n;
// int spaces = 0;

// int current_number_of_line = 1;

// while(current_number_of_line <= total_number_of_lines){
//     // print spaces
//     for(int i=1; i<=spaces; i++){
//         System.out.print("  ");
//     }

//     // print stars 
//     for(int i=1; i<=stars; i++){
//         System.out.print("* ");
//     }

//     // prepare for next line 
//     System.out.println();
//     stars--;
//     spaces++;
//     current_number_of_line++;
// }

class Main {
    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int n = scn.nextInt();

        int total_number_of_lines = 2*n - 1;

        int stars = 1;
        int spaces = n-1;

        int current_number_of_line = 1;

        while(current_number_of_line <= total_number_of_lines){
            // print spaces 
            for(int i=1; i<=spaces; i++){
                System.out.print("  ");
            }

            // print stars
            for(int i=1; i<=stars; i++){
                System.out.print("* ");
            }

            // prepare for next line 
            if(current_number_of_line < n){
                spaces--;
                stars = stars + 2;
            } else {
                spaces++;
                stars = stars - 2;
            }
            
            System.out.println();
            current_number_of_line++;
        }        
    }
}
Editor is loading...
Leave a Comment