Java Program to Print Pyramid with Decreasing Character Pattern

Printing Pyramid with Decreasing Character Pattern

In this program we are going to see how to print the triangle with increasing character pattern.

Let’s see the example first.

Example-1

When row value=6
         
        F
       E F
     D E F
   C D E F
  B C D E F
A B C D E F
Example-2:

When row value=5

      E
    D E
   C D E
  B C D E
A B C D E

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

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

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 new line.

JAVA CODE:

import java.util.*;

public class Main
{
    public static void main(String[] args)
    {   
        //Scanner class object created
        Scanner scan=new Scanner(System.in);
        
        // Enter number of rows as 
        // input from the User
        System.out.print("Rows : ");
        int row_count=scan.nextInt();

        // for loop to print all the rows
        for (int row = row_count; row >= 0; row--)
        {
            int c = 65;
            // for loop to print column values
            // here printing space as column valu
            for (int col = 0; col < row; col++)
            {
                System.out.print(" ");
            }
            //for loop to print column values
            //here printing character as column valu
            for (int k = row; k < row_count; k++)
            {
                System.out.print((char) (c + k) + " ");
            }
            // one row printed
            // moving to next line
            System.out.println();
        }

    }
}
Output:

Rows : 5

     E
   D E
  C D E
 B C D E
A B C D E

Related Java Star Pattern Programs: