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