Program to print H star pattern in c; Through this tutorial, we will learn how to print H star patterns using for loop and while loop in c programs.
Programs to Print H Star Pattern in C
- C Program to Print H Star Pattern using For Loop
- C Program to Print H Star Pattern using While Loop
C Program to Print H Star Pattern using For Loop
#include <stdio.h>
int main()
{
int rows, i, j, k, l;
printf("Please Enter H Pattern Rows = ");
scanf("%d", &rows);
printf("Printing H Star Pattern\n");
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
{
printf("*");
}
for (k = i * 2; k <= rows * 2 - 1; k++)
{
printf(" ");
}
for (l = 1; l <= i; l++)
{
printf("*");
}
printf("\n");
}
for (i = 1; i <= rows - 1; i++)
{
for (j = rows - 1; j >= i; j--)
{
printf("*");
}
for (k = 1; k <= i * 2; k++)
{
printf(" ");
}
for (l = rows - 1; l >= i; l--)
{
printf("*");
}
printf("\n");
}
}
The Output of the above c program; is as follows:
Please Enter H Pattern Rows = 5 Printing H Star Pattern * * ** ** *** *** **** **** ********** **** **** *** *** ** ** * *
C Program to Print H Star Pattern using While Loop
#include <stdio.h>
int main()
{
int rows, i, j, k, l;
printf("Please Enter H Pattern Rows = ");
scanf("%d", &rows);
printf("Printing H Star Pattern\n");
i = 1;
while (i <= rows)
{
j = 1;
while (j <= i)
{
printf("*");
j++;
}
k = i * 2;
while (k <= rows * 2 - 1)
{
printf(" ");
k++;
}
l = 1;
while (l <= i)
{
printf("*");
l++;
}
printf("\n");
i++;
}
i = 1;
while (i <= rows - 1)
{
j = rows - 1;
while (j >= i)
{
printf("*");
j--;
}
k = 1;
while (k <= i * 2)
{
printf(" ");
k++;
}
l = rows - 1;
while (l >= i)
{
printf("*");
l--;
}
printf("\n");
i++;
}
}
The Output of the above c program; is as follows:
Please Enter H Pattern Rows = 5 Printing H Star Pattern * * ** ** *** *** **** **** ********** **** **** *** *** ** ** * *