Java Program to Print Alphabet A character Pattern

Program to Print Alphabet A character Pattern

In the previous article, we have discussed Java Program to Print Alphabet T Character Pattern

In this article we are going to see how to print the alphabet A character pattern.

Output:

Enter rows : 8

ABC 
A     D
A     D
ABCD
A     D
A     D
A     D

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:

  • We will take the number of rows as 8 and store it in n.
  • First we will use a for loop to print the vertical lines
  • We are going to use the if..else condition to print the horizontal line.
  • After each iteration we will print a new line.

Java Code to Print Alphabet A character Pattern

import java.util.Scanner;
public class pattern  
{  
    public static void main(String[] args)  
    {  
        int ascii=65;
        Scanner scan = new Scanner(System.in);
        //Taking input as 8 for our A
        System.out.print("Enter rows : ");
        int r, c, rows= scan.nextInt();  
        // Outer for loop 
        for (r = 0; r<=rows; r++)   
        {  
        // Inner for loop 
        for (c = 0; c<= rows / 2; c++)   
        {  
            //To print the vertical lines 
            if ((c == 0 || c == rows / 2) && r != 0 ||  
            //Prints the first line 
            r == 0  && c != rows / 2 ||   
            //prints the middle line
            r == rows / 2)   
                System.out.print((char)(c+ascii));  
            else  
                System.out.print(" ");  
        }  
        //Prints new line
        System.out.println();  
        }  
    }  
}
Output:

Enter rows : 8

ABC 
A     D
A     D
ABCD
A     D
A     D
A     D

C Code to Print Alphabet A character Pattern

#include <stdio.h>
int main(int argc, char const *argv[])
{
    int ascii=65;
    int r, c, rows;
    //Taking row as input from user
    printf("Enter rows : ");
    scanf("%d", &rows);
    // Outer for loop
    for (r = 0; r <= rows; r++)
    {
        // Inner for loop
        for (c = 0; c <= rows / 2; c++)
        {
            //To print the vertical lines
            if ((c == 0 || c == rows / 2) && r != 0 ||
                //Prints the first line
                r == 0 && c != rows / 2 ||
                //prints the middle line
                r == rows / 2)
                printf("%c",(c+ascii));
            else
                printf(" ");
        }
        //Prints new line
        printf("\n");
    }
    return 0;
}
Output:

Enter rows : 8

ABC 
A     D
A     D
ABCD
A     D
A     D
A     D

C++ Code to Print Alphabet A character Pattern

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    int ascii=65;
    int r, c, rows;
    //Taking row as input from user
    cout << "Enter rows : ";
    cin >> rows;
    // Outer for loop
    for (r = 0; r <= rows; r++)
    {
        // Inner for loop
        for (c = 0; c <= rows / 2; c++)
        {
            //To print the vertical lines
            if ((c == 0 || c == rows / 2) && r != 0 ||
                //Prints the first line
                r == 0 && c != rows / 2 ||
                //prints the middle line
                r == rows / 2)
                cout << (char)(c+ascii);
            else
                cout << " ";
        }
        //Prints new line
        cout << endl;
    }
    return 0;
}
Output:

Enter rows : 8

ABC 
A     D
A     D
ABCD
A     D
A     D
A     D

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: