Problem: Print a Character Triangle
Print the
characters from ‘a’ to ‘z’ on the console on a single line, separated by a
space. Use a for-loop. Note: you can
directly declare and increment char in
the for-loop. for (char c = ‘a’; …)
import java.util.Scanner; public class PrintACharachterTriangle { 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(); } } }
No comments:
Post a Comment