Java Program to Print Pyramid with Column wise Increasing Character Pattern

Program to Print Pyramid with Column wise Increasing Character Pattern

In the previous article, we have discussed Java Program to Print Hollow Diamond within Rectangle Character Pattern

In this program we are going to see how to print the pyramid column wise increasing character pattern.

Example-1

When row value=5

        A   
      ABA  
    ABCBA 
  ABCDCBA
ABCDEDCBA
Example-2:

When row value=9

                A
              ABA
            ABCBA
          ABCDCBA
        ABCDEDCBA
      ABCDEFEDCBA
    ABCDEFGFEDCBA
  ABCDEFGHGFEDCBA
ABCDEFGHIHGFEDCBA

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

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Approach:

  • Enter total row and store it in an integer variable row_count.
  • Take one outer for loop to iterate the rows.
  • Inside the for loop, take three inner for loops, one to print the space and the other for characters.
  • After each iteration print a new line.

Java Code to Print Pyramid with Column wise Increasing Character Pattern

import java.util.Scanner;
class Main
{
public static void main(String[] args)  
{  
    Scanner scan = new Scanner(System.in);

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

    int row, col;
    //Outer for loop to iterate the rows
    for (row = 1; row <= row_count; row++)
    {
        //Loop to print space
        for (col = 1;col <=row_count-row;col++)
        {
            System.out.print(" ");
        }
        //Loop to print char
        for (col = 1; col <= row; col++)
        {
            System.out.print((char)(col+64));
        }
        //Loop to print char
        for ( col = row-1; col >= 1; col--)
        {
            System.out.print((char)(col+64));
        }
        //Prints a newline
        System.out.println();
    }
}  

}


Output

Rows : 9

              A
            ABA
          ABCBA
        ABCDCBA
      ABCDEDCBA
     ABCDEFEDCBA
   ABCDEFGFEDCBA
 ABCDEFGHGFEDCBA
ABCDEFGHIHGFEDCBA

C Code to Print Pyramid with Column wise Increasing Character Pattern

#include <stdio.h>

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

    int row, col;

    // Outer for loop to iterate the rows
    for (row = 1; row <= row_count; row++)
    {
        //Loop to print space
        for (col = 1; col <= row_count - row; col++)
        {
            printf(" ");
        }
        //Loop to print char
        for (col = 1; col <= row; col++)
        {
            printf("%c", (col + 64));
        }
        //Loop to print char
        for (col = row - 1; col >= 1; col--)
        {
            printf("%c", (col + 64));
        }
        //prints a newline
        printf("\n");
    }

    return 0;
}

Output:

Rows : 9

                A
              ABA
            ABCBA
          ABCDCBA
        ABCDEDCBA
      ABCDEFEDCBA
    ABCDEFGFEDCBA
  ABCDEFGHGFEDCBA
ABCDEFGHIHGFEDCBA

C++ Code to Print Pyramid with Column wise Increasing Character Pattern

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    //Taking number of rows as input from the user
    cout << "Rows : ";
    int row_count;
    cin >> row_count;

    int row, col;

    // Outer for loop to iterate the rows
    for (row = 1; row <= row_count; row++)
    {
        //Loop to print space
        for (col = 1; col <= row_count - row; col++)
        {
            cout << " ";
        }
        //Loop to print char
        for (col = 1; col <= row; col++)
        {
            cout << (char)(col + 64);
        }
        //Loop to print char
        for (col = row - 1; col >= 1; col--)
        {
            cout << (char)(col + 64);
        }
        // prints a newline
        cout << endl;
    }
    return 0;
}

Output:

Rows : 9

                A
              ABA
            ABCBA
          ABCDCBA
        ABCDEDCBA
      ABCDEFEDCBA
    ABCDEFGFEDCBA
  ABCDEFGHGFEDCBA
ABCDEFGHIHGFEDCBA

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: