Java Program to Print Mirrored Right-Angled Triangle with Repeated Character (Decreasing Order) Pattern

Program to Print Mirrored Right-Angled Triangle with Repeated Character (Decreasing Order) Pattern

In the previous article, we have discussed Java Program to Print Mirrored Right-Angled Triangle with Repeated Character (Increasing Order) Pattern

In this program we are going to see how to print a mirrored right-angled triangle character pattern in decreasing order.

Example-1

When character=p
and row value=4

       p
     oo
   nnn
mmm
Example-2:

When character=G 

and row value=5

          E
       DD
      CCC
    BBBB
 AAAAA

Now, let’s see the actual program to print it.

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Approach:

  • Enter the character and store it in a variable c.
  • Then enter total row and store it in an integer variable row_count.
  • Take one outer for loop to iterate the rows.
  • Take one inner loop to iterate the columns and print the character.
  • After each iteration print a newline.

Java Code to Print Mirrored Right-Angled Triangle with Repeated Character (Decreasing Order) Pattern

import java.util.Scanner;
class Main
{
public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    //Taking character to be printed as input from the user
    System.out.print("Character : ");
    char c = scan.next().charAt(0);

    //Taking number of rows as input from the user
    System.out.print("Rows : ");
    int row_count = scan.nextInt();

    int row, col, hold=row_count-1;
    //Converting the character to its ascii value
    int asciiValue = (int) c;

    //Checks whether the character is a letter or not
    if(asciiValue>=65&&asciiValue<=122)
    {
        
        //Outer loop to iterate rows
        for (row = 1; row <= row_count; row++)
        {
            //Inner loop to print space
            for (col = 1; col <=hold; col++)
            {
                System.out.print(" ");
            }
            hold--;
            //Inner loop to print character
            for(col = 1; col<=row; col++)
            {
                System.out.print(c);
                
            }
            c--;
            //Prints a newline
            System.out.println();
        }
    }
}
}

Output

Character : E
Rows : 5

         E
      DD
     CCC
   BBBB
AAAAA

C Code to Print Mirrored Right-Angled Triangle with Repeated Character (Decreasing Order) Pattern

#include <stdio.h>

int main()
{
    //Taking character to be printed as input from the user
    printf("Character : ");
    char c;
    scanf("%c", &c);

    //Taking number of rows as input from the user
    printf("Rows : ");
    int row_count;
    scanf("%d", &row_count);

    int row, col, hold = row_count - 1;
    //Converting the character to its ascii value
    int asciiValue = (int)c;

    //Checks whether the character is a letter or not
    if (asciiValue >= 65 && asciiValue <= 122)
    {
        //Outer loop to iterate rows
        for (row = 1; row <= row_count; row++)
        {
            //Inner loop to print space
            for (col = 1; col <= hold; col++)
            {
                printf(" ");
            }
            hold--;
            //Inner loop to print character
            for (col = 1; col <= row; col++)
            {
                printf("%c", c);
            }
            c--;
            //Prints a newline
            printf("\n");
        }
    }
    return 0;
}
Output

Character : E
Rows : 5

         E
      DD
     CCC
    BBBB
 AAAAA

C++ Code to Print Mirrored Right-Angled Triangle with Repeated Character (Decreasing Order) Pattern

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    //Taking character to be printed as input from the user
    cout << "Character : ";
    char c;
    cin >> c;

    //Taking number of rows as input from the user
    cout << "Rows : ";
    int row_count;
    cin >> row_count;

    int row, col, hold = row_count - 1;
    //Converting the character to its ascii value
    int asciiValue = (int)c;

    //Checks whether the character is a letter or not
    if (asciiValue >= 65 && asciiValue <= 122)
    {
        //Outer loop to iterate rows
        for (row = 1; row <= row_count; row++)
        {
            //Inner loop to print space
            for (col = 1; col <= hold; col++)
            {
                cout << " ";
            }
            hold--;
            //Inner loop to print character
            for (col = 1; col <= row; col++)
            {
                cout << c;
            }
            c--;
            //Prints a newline
            cout << endl;
        }
    }
    return 0;
}

Output:

Character : E
Rows : 5

          E
       DD
     CCC
   BBBB
AAAAA

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Java Character Pattern Programs: