Java Program to Print Alphabet A Star Pattern

Program to Print Alphabet A Star Pattern

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

Example-1

When row value=8
****
*       *
*       *
*       *
*****
*       *
*       *
*       *
Example-2

When row value=9
*****
*        *
*        *
*        *
*        *
******
*        *
*        *
*        *
*        *
*        *

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

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:

Method-1 : Static Star Character

import java.util.Scanner;
public class pattern  
{  
    public static void main(String[] args)  
    {  
        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("*");  
            else  
                System.out.print(" ");  
        }  
        //Prints new line
        System.out.println();  
        }  
    }  
}  

Enter rows : 10
***** 
*      *
*      *
*      *
*      *
******
*      *
*      *
*      *
*      *
*      *

Method-2 : User Input Character

import java.util.Scanner;
public class pattern  
{  
    public static void main(String[] args)  
    {  
        int r, c;
        Scanner scan = new Scanner(System.in);
        
        System.out.print("Enter rows : ");
        int rows= scan.nextInt(); 
        
        System.out.print("Enter any character : ");
        char s= scan.next().charAt(0); 
        // 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(s);  
            else  
                System.out.print(" ");  
        }  
        //Prints new line
        System.out.println();  
        }  
    }  
}  

Output:

Enter rows : 10
Enter any character : a
aaaaa 
a       a
a       a
a       a
a       a
aaaaaa
a       a
a       a
a       a
a       a
a       a

C Code:

#include <stdio.h>
int main(int argc, char const *argv[])
{
    
    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("*");
            else
                printf(" ");
        }
        //Prints new line
        printf("\n");
    }
    return 0;
}
Output:

Enter rows : 10
Enter any character : a
aaaaa 
a       a
a       a
a       a
a       a
aaaaaa
a       a
a       a
a       a
a       a
a       a

C++ Code:

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
 
    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 << "*";
            else
                cout << " ";
        }
        //Prints new line
        cout << endl;
    }
    return 0;
}
Output:

Enter rows : 10
Enter any character : a
aaaaa 
a       a
a       a
a       a
a       a
aaaaaa
a       a
a       a
a       a
a       a
a       a

Related Java Star Pattern Programs: