dimanche 5 mars 2017

Is there a way I can center this?

I just finshed testing Pascal's triangle and want to make it look nice. Is there a way to center each row using printf? Right now it looks like this:
1
11
121
1331
14641
161051
1771561
19487171

I want it so that it looks a equilateral triangle, thanks!

My Code

public static void main(String[] args) {
    // TODO Auto-generated method stub
    int sum = 0;

    Scanner reader = new Scanner(System.in);
    System.out.print("Enter the amount of rows: ");
    int row = reader.nextInt();

    for(int i = 0; i < row; i++){
        System.out.println();
        sum = (int) Math.pow(11, i);
        System.out.printf("%10d \n", sum);
    }
}

Aucun commentaire:

Enregistrer un commentaire