Write a 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"); } }
In this C example, we used the while loop to print the H star pattern.
#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++; } }
Please Enter H Pattern Rows = 11
Printing H Star Pattern
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
******** ********
********* *********
********** **********
**********************
********** **********
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
C Program to Print H Star Pattern using a do 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; do { j = 1; do { printf("*"); } while (++j <= i); k = i * 2; while (k <= rows * 2 - 1) { printf(" "); k++; } l = 1; do { printf("*"); } while (++l <= i); printf("\n"); } while (++i <= rows); i = 1; do { j = rows - 1; do { printf("*"); } while (--j >= i); k = 1; do { printf(" "); } while (++k <= i * 2); l = rows - 1; do { printf("*"); } while (--l >= i); printf("\n"); } while (++i <= rows - 1); }
Please Enter H Pattern Rows = 13
Printing H Star Pattern
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
******** ********
********* *********
********** **********
*********** ***********
************ ************
**************************
************ ************
*********** ***********
********** **********
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
Here, we created HPattern function that run the repeated for loops. This C program allows the user to enter the character and print the given character’s H pattern using that function.
#include <stdio.h> void HPattern(int rows, char ch); int main() { int rows; char ch; printf("Enter Character for H Pattern = "); scanf("%c", &ch); printf("Please Enter H Pattern Rows = "); scanf("%d", &rows); printf("Printing H Pattern\n"); HPattern(rows, ch); } void HPattern(int rows, char ch) { int i, j, k, l; for (i = 1; i <= rows; i++) { for (j = 1; j <= i; j++) { printf("%c", ch); } for (k = i * 2; k <= rows * 2 - 1; k++) { printf(" "); } for (l = 1; l <= i; l++) { printf("%c", ch); } printf("\n"); } for (i = 1; i <= rows - 1; i++) { for (j = rows - 1; j >= i; j--) { printf("%c", ch); } for (k = 1; k <= i * 2; k++) { printf(" "); } for (l = rows - 1; l >= i; l--) { printf("%c", ch); } printf("\n"); } }
Enter Character for H Pattern = $
Please Enter H Pattern Rows = 14
Printing H Pattern
$ $
$$ $$
$$$ $$$
$$$$ $$$$
$$$$$ $$$$$
$$$$$$ $$$$$$
$$$$$$$ $$$$$$$
$$$$$$$$ $$$$$$$$
$$$$$$$$$ $$$$$$$$$
$$$$$$$$$$ $$$$$$$$$$
$$$$$$$$$$$ $$$$$$$$$$$
$$$$$$$$$$$$ $$$$$$$$$$$$
$$$$$$$$$$$$$ $$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$ $$$$$$$$$$$$$
$$$$$$$$$$$$ $$$$$$$$$$$$
$$$$$$$$$$$ $$$$$$$$$$$
$$$$$$$$$$ $$$$$$$$$$
$$$$$$$$$ $$$$$$$$$
$$$$$$$$ $$$$$$$$
$$$$$$$ $$$$$$$
$$$$$$ $$$$$$
$$$$$ $$$$$
$$$$ $$$$
$$$ $$$
$$ $$
$ $