Java Program to Print Reverse K Shape Character Pattern

Program to Print Reverse K Shape Character Pattern

In the previous article, we have discussed Java Program to Print K Shape with Decreasing Character Pattern

In this program we are going to see how to print the Reverse K shape character pattern.

Example-1

When row value=6

ABC
  BC
    C
    C
  BC
ABC
Example-2:

When row value=10

ABCDE
  BCDE
    CDE
      DE
        E
        E
      DE
    CDE
  BCDE
ABCDE

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

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Approach:

  • Enter total row and store it in an integer variable row_count.
  • Take two outer for loops, one for the upper half and the other for the bottom.
  • Inside both the loops, take two inner for loops to print the space and the characters.
  • After each iteration print a new line.

Java Code to Print Reverse K Shape 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 and
        //then dividing it by two to get the size of the halves
        System.out.print("Rows(Enter even number) : ");
        int row_count = scan.nextInt();
        row_count/=2;
        
    
        int row, col, alphaAscii;
        //row, col are iterator and 
        //the alphaAscii is the 
        //ASCII value holder initialized to hold 'A'
    
        //loop to print upper part of the pattern
        for (row = 0; row <= row_count - 1; row++)
        {
            //Resetting the value to 'A' after every iteration
            alphaAscii = 65;
            
            for (col = 0; col < row; col++)
            {
                //Inner loop to print space
                System.out.print(" ");
            }
            for (col = row; col <= row_count-1; col++)
            {
            
                System.out.print((char)(alphaAscii+col));
            }
            System.out.println();
        }
        
        //loop to print lower part of the pattern
        for (row = row_count - 1; row >= 0; row--)
        {
            //Resetting the value to 'A' after every iteration
            alphaAscii = 65;
            
            for (col = 0; col < row; col++)
            {
                    //Inner loop to print space
                    System.out.print(" ");
            }
             //Inner loop to print character
            for (col = row; col <= row_count-1; col++)
            {
                System.out.print((char)(alphaAscii+col));
            }
            System.out.println();
        }
    }
}

Output:

Rows(Enter even number) : 10

ABCDE
  BCDE
    CDE
      DE
        E
        E
      DE
    CDE
  BCDE
ABCDE

C Code to Print Reverse K Shape Character Pattern

#include <stdio.h>

int main()
{
    printf("Rows(Enter odd number) : ");
    int row_count;
    scanf("%d", &row_count);
    row_count /= 2;
    //Taking number of rows as input from the user and
    //then dividing it by two to get the size of the halves

    int row, col, alphaAscii;
    //row, col are iterator and the alphaAscii is the ASCII value holder

    for (row = 0; row <= row_count - 1; row++)
    {
        //loop to print upper part of the pattern
        alphaAscii = 65;
        //Resetting the value to 'A' after every iteration
        for (col = 0; col < row; col++)
        {
            //Inner loop to print space
            printf(" ");
        }
        for (col = row; col <= row_count - 1; col++)
        {
            //Inner loop to print character
            printf("%c", (char)(alphaAscii + col));
        }
        printf("\n");
    }
    for (row = row_count - 1; row >= 0; row--)
    {
        //loop to print lower part of the pattern
        alphaAscii = 65;
        //Resetting the value to 'A' after every iteration
        for (col = 0; col < row; col++)
        {
            //Inner loop to print space
            printf(" ");
        }
        for (col = row; col <= row_count - 1; col++)
        {
            //Inner loop to print character
            printf("%c", (char)(alphaAscii + col));
        }
        printf("\n");
    }
    return 0;
}

Output:

Rows(Enter even number) : 10

ABCDE
  BCDE
    CDE
      DE
        E
        E
      DE
    CDE
  BCDE
ABCDE

C++ Code to Print Reverse K Shape Character Pattern

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    cout << "Rows(Enter even number) : ";
    int row_count;
    cin >> row_count;
    row_count /= 2;
    //Taking number of rows as input from the user and
    //then dividing it by two to get the size of the halves

    int row, col, alphaAscii = 65;
    //row, col are iterator and the alphaAscii is the ASCII value holder

    for (row = 0; row <= row_count - 1; row++)
    {
        //loop to print upper part of the pattern
        alphaAscii = 65;
        //Resetting the value to 'A' after every iteration
        for (col = 0; col < row; col++)
        {
            //Inner loop to print space
            cout << " ";
        }
        for (col = row; col <= row_count - 1; col++)
        {
            //Inner loop to print character
            cout << (char)(alphaAscii + col);
        }
        cout << endl;
    }
    for (row = row_count - 1; row >= 0; row--)
    {
        //loop to print lower part of the pattern
        alphaAscii = 65;
        //Resetting the value to 'A' after every iteration
        for (col = 0; col < row; col++)
        {
            //Inner loop to print space
            cout << " ";
        }
        for (col = row; col <= row_count - 1; col++)
        {
            //Inner loop to print character
            cout << (char)(alphaAscii + col);
        }
        cout << endl;
    }

    return 0;
}
Output:

Rows(Enter even number) : 10

ABCDE
  BCDE
    CDE
      DE
        E
        E
      DE
    CDE
  BCDE
ABCDE

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: