C Program to Print H Star Pattern

In this tutorial, i am going to show you how to print H star patterns with the help of for loop and while loop in c programs.

All C Programs to Print H Star Pattern

  • 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 result 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 result of the above c program; is as follows:

Please Enter H Pattern Rows = 5
Printing H Star Pattern
*        *
**      **
***    ***
****  ****
**********
****  ****
***    ***
**      **
*        *

More C Programming Tutorials

Leave a Comment