C Program to Print Alphabet A Star Pattern

Write a C program to print the Alphabet A pattern of stars using for loop, while loop, and functions with an example. The below program uses the nested for loops to iterate the rows and columns. The if else condition checks the top, outer edges, and middle lines to print stars.

#include<stdio.h>
 
int main(void)
{
    int i, j, rows;
    printf("Enter Rows to Print A =  ");
    scanf("%d", &rows);
    
    for (int i = 0 ; i < rows; i++ )
    {
        for (int j = 0 ; j <= rows/2; j++ )
        {
            if ((i == 0 && j != 0 && j != rows/2) || i == rows / 2)
                printf("*");
            else if ((j == 0 || j == rows / 2) && i != 0)
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
}
C Program to Print Alphabet A Star Pattern

C Program to Print Alphabet A Star Pattern using while loop

In this program, we replaced the for loop with the while loop and also replaced the Else if statement with If else to print the Alphabet A pattern of stars.

#include<stdio.h>
 
int main(void)
{
    int rows;
    printf("Enter Rows to Print A =  ");
    scanf("%d", &rows);
    
    int i = 0 ;
    while (i < rows)
    {
        int j = 0 ;
        while (j <= rows/2 )
        {
            if (((i == 0 && j != 0 && j != rows/2) || i == rows / 2)
                || ((j == 0 || j == rows / 2) && i != 0))
                printf("*");
            else
                printf(" ");
            j++;
        }
        printf("\n");
        i++;
    }
}

Output

Enter Rows to Print A =  7
 ** 
*  *
*  *
****
*  *
*  *
*  *

This example uses the do while loop and prints the Alphabet A Star Pattern.

#include<stdio.h>
 
int main(void)
{
    int rows;
    printf("Enter Rows to Print A =  ");
    scanf("%d", &rows);
    
    int i = 0 ;
    do
    {
        int j = 0 ;
        do
        {
            if (((i == 0 && j != 0 && j != rows/2) || i == rows / 2)
                || ((j == 0 || j == rows / 2) && i != 0))
                printf("*");
            else
                printf(" ");
            j++;
        } while (j <= rows/2 );
        printf("\n");
        i++;
    } while (i < rows);
}

Output

Enter Rows to Print A =  11
 **** 
*    *
*    *
*    *
*    *
******
*    *
*    *
*    *
*    *
*    *

In the below program, we created a function to put the for loop logic, and it accepts the rows and any character and uses the symbol to print the Alphabet A pattern of stars.

#include<stdio.h>
void loopLogic(int rows, char ch)
{
    for (int i = 0 ; i < rows; i++ )
    {
        for (int j = 0 ; j <= rows/2; j++ )
        {
            if (((i == 0 && j != 0 && j != rows/2) || i == rows / 2)
                || ((j == 0 || j == rows / 2) && i != 0))
                printf("%c", ch);
            else
                printf(" ");
        }
        printf("\n");
    }
}
int main(void)
{
    int rows;
    char ch;
    
    printf("Enter Character = ");
    scanf("%c", &ch);
    
    printf("Enter Rows to Print A =  ");
    scanf("%d", &rows);
    
    loopLogic(rows, ch);
}

Output

Enter Character = $
Enter Rows to Print A =  14
 $$$$$$ 
$      $
$      $
$      $
$      $
$      $
$      $
$$$$$$$$
$      $
$      $
$      $
$      $
$      $
$      $