Java Online Compiler
Example: Right-Aligned Alphabet Triangle in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Right-Aligned Alphabet Triangle import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the number of rows for the right-aligned triangle: "); int rows = scanner.nextInt(); // Outer loop for each row for (int i = 0; i < rows; i++) { // Print leading spaces for right alignment for (int j = 0; j < rows - 1 - i; j++) { System.out.print(" "); } // Print increasing alphabets for the current row for (int k = 0; k <= i; k++) { System.out.print((char) ('A' + k)); } System.out.println(); // Move to the next line } scanner.close(); } }
Output
Clear
ADVERTISEMENTS