[Solved] Print a Character Triangle - Java Task

  

4
Topic starter

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

1 Answer
2

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();
    }
}
Share: