Java Program to Print Diamond Character Pattern

Program to Print Diamond Character Pattern

In the previous article, we have discussed Java Program to Print Inverted Right-Angled Triangle with Row wise Decreasing Character Pattern

In this program we are going to see how to print the diamond character pattern.

Example-1

When row value=5

   A
  A B
 A B C
  A B
   A

 

Example-2:

When row value=9

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

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

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

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 newline.

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

    //loop to print lower part of the pattern
    for(row = 0;row<=row_count;row++)
    {
        //Resetting the alphabet to 'A' for every iteration
        alphaAscii = 65;
        //Inner loop to print space
        for(col = -1; col<=row;col++)
        {
            // printing space
            System.out.print(" ");
        }
         //Inner loop to print characters starting from 'A'
        for(col = 0; col<row_count-row; col++)
        {
            // printing character
            System.out.print((char) (alphaAscii+col)+" ");
        }
        System.out.println();
    }
}
}

Output:

Rows(Enter odd number) : 9

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

C Code to Print Diamond Character Pattern

#include <stdio.h>

int main()
{
    //Taking number of rows as input from the user and
    printf("Rows(Enter odd number) : ");
    int row_count;
    scanf("%d", &row_count);
    //then dividing it by two to get the size of the halves
    row_count /= 2;
    
    //row, col are iterator and 
    //the alphaAscii is the ASCII value holder
    int row, col, alphaAscii;
    
    //loop to print upper part of the pattern
    for (row = 0; row <= row_count; row++)
    {
        //Resetting the alphabet to 'A' for every iteration
        alphaAscii = 65;
        //Inner loop to print space
        for (col = row_count; col >= row; col--)
        {
            printf(" ");
        }
        //Inner loop to print characters starting from 'A'
        for (col = 0; col <= row; col++)
        {
            printf("%c ", (char)(alphaAscii + col));
        }
        printf("\n");
    }
    //loop to print lower part of the pattern
    for (row = 0; row <= row_count; row++)
    {
         //Resetting the alphabet to 'A' for every iteration
        alphaAscii = 65;
        //Inner loop to print space
        for (col = -1; col <= row; col++)
        {
            printf(" ");
        }
        //Inner loop to print characters starting from 'A'
        for (col = 0; col < row_count - row; col++)
        {
            printf("%c ", (char)(alphaAscii + col));
        }
        printf("\n");
    }
    return 0;
}
Output:

Rows(Enter odd number) : 9

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

C++ Code to Print Diamond Character Pattern

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    cout << "Rows(Enter odd 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;
    //row, col are iterator and the alphaAscii is the ASCII value holder

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

        for (col = 0; col <= row; col++)
        {
            //Inner loop to print characters starting from 'A'
            cout << (char)(alphaAscii + col) << " ";
        }
        cout << endl;
    }

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

        for (col = 0; col < row_count - row; col++)
        {
            //Inner loop to print characters starting from 'A'
            cout << (char)(alphaAscii + col) << " ";
        }
        cout << endl;
    }
    return 0;
}

Output:

Rows(Enter odd number) : 9

     A
    A B
   A B C
  A B C D
 A B C D E
  A B C D
   A B C
    A 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: