In this article, we will show how to write a C program to print the Alphabet H 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++ )
{
printf("*");
for (int j = 0 ; j < rows; j++ )
{
if (i == rows / 2 || j == rows - 1)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
Enter Rows = 15
* *
* *
* *
* *
* *
* *
* *
**********
* *
* *
* *
* *
* *
* *
* *
Instead of a for loop, this Alphabet H 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)
{
printf("*");
j = 0 ;
while ( j < rows )
{
if (i == rows / 2 || j == rows - 1)
{
printf("*");
}
else
{
printf(" ");
}
j++;
}
printf("\n");
i++;
}
return 0;
}
Enter Rows = 19
* *
* *
* *
* *
* *
* *
* *
* *
* *
************
* *
* *
* *
* *
* *
* *
* *
* *
* *
In this C program, we create an alphabetHPat function that accepts those values and prints the Alphabet H Pattern of the given char.
#include <stdio.h>
void alphabetHPat(int rows, char a)
{
for (int i = 0 ; i < rows; i++ )
{
printf("%c", a);
for (int j = 0; j < rows; j++)
{
if (i == rows / 2 || j == rows - 1)
{
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);
alphabetHPat(rows, a);
return 0;
}