4
28/10/2024 7:08 pm
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:
1 Answer
2
28/10/2024 7:08 pm
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(); } }