C Program to Print Alphabet G Pattern

In this article, we will show how to write a C program to print the Alphabet G pattern or shape of stars using for loop, while loop, and functions.

#include <stdio.h>

int main()
{
int rows;

printf("Enter Rows = ");
scanf("%d",&rows);

for (int i = 0 ; i < rows; i++ )
{
for (int j = 0 ; j < 2 * rows; j++ )
{
if ((i == 0 || i == rows - 1) && (j == 0 || j == (2 * rows) - 2))
{
printf(" ");
}
else if ((j == 0) || (i == 0 && j <= rows) || (i == rows / 2 && j > rows / 2)
|| (i > rows / 2 && j == (2 * rows) - 1) || (i == rows - 1 && j < 2 * rows))
{
printf("*");
}
else
{
printf(" ");
}

}
printf("\n");
}

return 0;
}
Enter Rows = 17
 *****************                
*                                 
*                                 
*                                 
*                                 
*                                 
*                                 
*                                 
*        ***************
*                                *
*                                *
*                                *
*                                *
*                                *
*                                *
*                                *
 ********************

Instead of a for loop, this Alphabet G Pattern of stars program uses a while loop to iterate the rows and columns. For more star programs, click here.

#include <stdio.h>

int main()
{
int i, j, rows;

printf("Enter Rows = ");
scanf("%d",&rows);

i = 0 ;
while ( i < rows )
{
j = 0 ;
while ( j < (2 * rows) - 1)
{
if ((i == 0 || i == rows - 1) && (j == 0 || j == (2 * rows) - 3))
{
printf(" ");
}
else if ((j == 0) || (i == 0 && j <= rows) || (i == rows / 2 && j > rows / 2)
|| (i > rows / 2 && j == (2 * rows) - 3) || (i == rows - 1 && j < (2 * rows)- 2))
{
printf("*");
}
else
{
printf(" ");
}
j++;
}
printf("\n");
i++;
}

return 0;
}
Enter Rows = 15
 ***************             
*                            
*                            
*                            
*                            
*                            
*                            
*       ************
*                          * 
*                          * 
*                          * 
*                          * 
*                          * 
*                          * 
 ****************

In this C program, we create an alphabetGPat function that accepts those values and prints the Alphabet G shape or pattern of the given char.

#include <stdio.h>

void alphabetGPat(int rows, char a)
{
for (int i = 0 ; i < rows; i++ )
{
for (int j = 0; j < 2 * rows; j++)
{
if ((i == 0 || i == rows - 1) && (j == 0 || j == (2 * rows) - 2))
{
printf(" ");
}
else if ((j == 0) || (i == 0 && j <= rows)
|| (i == rows / 2 && j > rows / 2)
|| (i > rows / 2 && j == (2 * rows) - 1)
|| (i == rows - 1 && j < 2 * rows))
{
printf("%c", a);
}
else
{
printf(" ");
}

}
printf("\n");
}
}
int main()
{
int rows;
char a;

printf("Enter Alphabet = ");
scanf("%c", &a);

printf("Enter Rows = ");
scanf("%d",&rows);

alphabetGPat(rows, a);
return 0;
}
C Program to Print Alphabet G Pattern