Pattern:
ABCD
ABC
AB
A
The total number of rows is 4.
The numbers in each row start from 'A' and decrease in count as we move downward:
The outer loop runs from 0 to total_rows - 1.
The inner loop prints characters starting from 'A' up to total_rows - row characters.
Java |
public class Main { public static void main(String[] args) { int total_rows = 4; // Number of rows // Outer loop for rows (starting from 0) for (int row = 0; row < total_rows; row++) { // Inner loop for columns (printing letters) for (int col = 0; col < total_rows - row; col++) { System.out.print((char) ('A' + col)); } System.out.println(); // Move to the next line } } } |
C++ |
#include <iostream> using namespace std; int main() { int total_rows = 4; // Number of rows // Outer loop for rows (starting from 0) for (int row = 0; row < total_rows; row++) { // Inner loop for columns (printing letters) for (int col = 0; col < total_rows - row; col++) { cout << (char) ('A' + col); } cout << endl; // Move to the next line } return 0; } |
Python |
total_rows = 4 # Number of rows # Outer loop for rows (starting from 0) for row in range(total_rows): # Inner loop for columns (printing letters) for col in range(total_rows - row): print(chr(65 + col), end="") # Convert 65 to 'A' print() # Move to the next line |