Java Program to Print K Shape with Decreasing Character Pattern

Program to Print K Shape with Decreasing Character Pattern

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

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

Example-1

When row value=6

C B A
B A
A
A
B A
C B A
Example-2:

When row value=10

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

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

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

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 one inner loop to print the character.
  • After each iteration print a newline.

Java Code to Print K Shape with Decreasing Character Pattern

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

    System.out.print("Rows(Enter even number) : ");
    int row_count = scan.nextInt();
    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 = row_count - 1; row >= 0; row--)
    {
        //loop to print upper part of the pattern
        alphaAscii = 65;
        //Resetting the value to 'A' after every iteration
        for (col = row; col >= 0; col--)
        {
            System.out.print((char)(alphaAscii+col)+ " ");
        }
        System.out.println();
    }
    for (row = 0; row <= row_count - 1; row++)
    {
        //loop to print lower part of the pattern
        alphaAscii = 65;
        //Resetting the value to 'A' after every iteration
        for (col = row; col >= 0; col--)
        {
            System.out.print((char)(alphaAscii+col)+ " ");
        }
        System.out.println();
    }
}
}

Output:

Rows(Enter even number) : 10

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

C Code to Print K Shape with Decreasing 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 = 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 = row; col >= 0; col--)
        {
            printf("%c ", (char)(alphaAscii + col));
        }
        printf("\n");
    }
    for (row = 0; row <= row_count - 1; row++)
    {
        //loop to print lower part of the pattern
        alphaAscii = 65;
        //Resetting the value to 'A' after every iteration
        for (col = row; col >= 0; col--)
        {
            //Inner loop to print characters starting from 'A'
            printf("%c ", (char)(alphaAscii + col));
        }
        printf("\n");
    }
    return 0;
}
Output:

Rows(Enter even number) : 10

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

C++ Code to Print K Shape with Decreasing 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 = 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 = row; col >= 0; col--)
        {
            cout << (char)(alphaAscii + col) << " ";
        }
        cout << endl;
    }

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

    return 0;
}

Output:

Rows(Enter even number) : 10

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

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: