Write a C program to print 8 star pattern using for loop.
#include <stdio.h> int main() { int rows; printf("Please Enter 8 Star Pattern Rows = "); scanf("%d", &rows); printf("Printing 8 Pattern of Stars\n"); for (int i = 1; i <= rows * 2 - 1; i++) { if (i == 1 || i == rows || i == rows * 2 - 1) { for (int j = 1; j <= rows; j++) { if (j == 1 || j == rows) { printf(" "); } else { printf("*"); } } } else { for (int k = 1; k <= rows; k++) { if (k == 1 || k == rows) { printf("*"); } else { printf(" "); } } } printf("\n"); } }
C Program to Print 8 Star Pattern using a while loop
#include <stdio.h> int main() { int i, j, k, rows; printf("Please Enter 8 Star Pattern Rows = "); scanf("%d", &rows); printf("\n"); i = 1; while (i <= rows * 2 - 1) { if (i == 1 || i == rows || i == rows * 2 - 1) { j = 1; while (j <= rows) { if (j == 1 || j == rows) { printf(" "); } else { printf("*"); } j++; } } else { k = 1; while (k <= rows) { if (k == 1 || k == rows) { printf("*"); } else { printf(" "); } k++; } } printf("\n"); i++; } }
Please Enter 8 Star Pattern Rows = 10
********
* *
* *
* *
* *
* *
* *
* *
* *
********
* *
* *
* *
* *
* *
* *
* *
* *
********
This C program prints the 8 digit star pattern using a do while loop.
#include <stdio.h> int main() { int i, j, k, rows; printf("Please Rows = "); scanf("%d", &rows); printf("\n"); i = 1; do { if (i == 1 || i == rows || i == rows * 2 - 1) { j = 1; do { if (j == 1 || j == rows) { printf(" "); } else { printf("*"); } } while (++j <= rows); } else { k = 1; do { if (k == 1 || k == rows) { printf("*"); } else { printf(" "); } } while (++k <= rows); } printf("\n"); } while (++i <= rows * 2 - 1); }
Please Rows = 12
**********
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
**********
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
**********
In this C example, the printingPattern8 function allows rows & characters and prints the 8 digit pattern of a given character.
#include <stdio.h> void printingPattern8(int rows, char ch); int main() { int rows; char ch; printf("Enter Character for 8 Pattern = "); scanf("%c", &ch); printf("Please Enter 8 Pattern Rows = "); scanf("%d", &rows); printf("Printing 8 Pattern of Stars\n"); printingPattern8(rows, ch); } void printingPattern8(int rows, char ch) { for (int i = 1; i <= rows * 2 - 1; i++) { if (i == 1 || i == rows || i == rows * 2 - 1) { for (int j = 1; j <= rows; j++) { if (j == 1 || j == rows) { printf(" "); } else { printf("%c", ch); } } } else { for (int k = 1; k <= rows; k++) { if (k == 1 || k == rows) { printf("%c", ch); } else { printf(" "); } } } printf("\n"); } }
Enter Character for 8 Pattern = ^
Please Enter 8 Pattern Rows = 14
Printing 8 Pattern of Stars
^^^^^^^^^^^^
^ ^
^ ^
^ ^
^ ^
^ ^
^ ^
^ ^
^ ^
^ ^
^ ^
^ ^
^ ^
^^^^^^^^^^^^
^ ^
^ ^
^ ^
^ ^
^ ^
^ ^
^ ^
^ ^
^ ^
^ ^
^ ^
^ ^
^^^^^^^^^^^^