settingsAccountsettings
Menusettings

Q: Print a Character Triangle - Java Task

+8 votes

Create a triangle of characters, based on input. Use java.util.Scanner and Integer.parseInt ().

Test your program with integers up to 26. Think why.

MUST LOOK LIKE THIS:

Print Character Triangle Java 1

Print Character Triangle Java 2

asked in Java category by user hues
edited by user golearnweb

1 Answer

+2 votes
 
Best answer

Here is my solution:

import java.util.Scanner;

public class PrintCharacterTriangle05 {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);

        int number = Integer.parseInt(console.nextLine());

        for (int i = 1; i <= number; i++) {
            for (int j = 0; j < i; j++) {
                System.out.print((char) (j + 97) + " ");
            }
            System.out.println();
        }

        for (int i = number - 1; i >= 0; i--) {
            for (int j = 0; j < i; j++) {
                System.out.print((char) (j + 97) + " ");
            }
            System.out.println();
        }
        System.out.println();
    }
}
answered by user paulcabalit
selected by user hues
Thanks you very much! nice!
...