C Program to print an 8 Star Pattern – In this article, we will detail in on the various ways to print an 8-star pattern in C programming.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.
The several ways explained in this piece are as follows:
- Using For Loop
- Using While Loop
- Using Do-While Loop
As the name suggests, an 8-Star pattern is a pattern that you have to make with the help of the “*” symbol. It gives out the shape of the mathematical digit 8.
It looks like this:
As you can see, you have to first enter the number of rows. Accordingly, your 8-Star pattern will be drawn.
THus, the ways to do the same in C programming are as follows:
Using For Loop
1) Read and store the entered number of rows and entered a symbol into the variables n, ch.
2) k is defined as k=n*2-1.
3) The outer for loop iterates through rows with the structure for(i=1;i<=k;i++)
4) If( i=1 or i=n or i=k) condition is true then
The 1st inner for loop iterates through columns with the structure for(j=1;j<=n;j++).
a) If(j=1 or j=n) is true then it prints space.
b) Otherwise, it prints symbol.
5) If (i=1 or i=n or i=k) is false then
The 2nd inner for loop iterates through columns with the structure for(j=1;j<=n;j++).
a) If( j=1 or j=n) the condition is true then it prints symbol.
b) Otherwise, it prints space.
6) Cursor comes to next line for each iteration of outer for loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
#include <stdio.h> #include <conio.h> int main() { int i,j,n,k; char ch; printf("Enter number of rows: "); scanf("%d%c",&n,&ch); printf("Enter the symbol: "); ch=getchar(); k=n*2-1; for(i=1;i<=k;i++) { if(i==1 || i==n || i==k) for(j=1;j<=n;j++) { if(j==1 || j==n) printf(" "); else printf("%c",ch); } else for(j=1;j<=n;j++) { if(j==1 || j==n) printf("%c",ch); else printf(" "); } printf("\n"); } return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 11 |
Enter number of rows: 5 Enter the symbol: * *** * * * * * * *** * * * * * * *** |
Using While Loop
1) k=n*2-1.
2) i=1, The outer while loop with the condition i<=k iterates through rows until the condition becomes false.
3) If (i=1 or i=n or i=k ) condition is true
a) The inner while loop iterates through columns until the condition j<=n becomes false.
It prints space if( j=1 or j=n)is true. Otherwise, it prints symbol.
j value increased by 1.
4) If(i=1 or i=n or i=k)condition is false
a) The 2nd inner while loop iterates through columns until the condition j<=n becomes false.
It prints symbol if(j=1 or j=n)is true.Otherwise, it prints space.
5) The cursor comes to next line.i value increased by 1 of the outer while loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
#include <stdio.h> #include <conio.h> int main() { int i,j,n,k; char ch; printf("Enter number of rows: "); scanf("%d%c",&n,&ch); printf("Enter the symbol: "); ch=getchar(); i=1; k=n*2-1; while(i<=k) { j=1; if(i==1 || i==n || i==k) { while(j<=n) { if(j==1 || j==n) printf(" "); else printf("%c",ch); j++; } } else { while(j<=n) { if(j==1 || j==n) printf("%c",ch); else printf(" "); j++; } } printf("\n"); i++; } return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 11 |
Enter number of rows: 5 Enter the symbol: . ... . . . . . . ... . . . . . . ... |
Using Do-While Loop
1) k=n*2-1.
2) i=1, The outer do-while loop iterates through rows.
3) If (j=1 or j=n or j=k) condition is true then
j=1, the inner do-while loop iterates through columns.
a) It prints space if j=1 or n.Otherwise, it prints symbol.
b) j value increased by 1.
Checks the condition j<=n.If the condition is true then inner loop iterates again.Repeats until the condition becomes false.
4) If(j=1 or j=n or j=k)condition is false then
j=1, the 2nd inner do-while loop iterates through columns.
a) It prints symbol if j=1 or n.Otherwise, it prints space.
b) j value increased by 1.
Checks the condition j<=n.If the condition is true then 2nd inner do-while loop iterates again until the condition becomes false.
5) The cursor comes to next line. i value increased by 1.
Checks the condition i<=k.If the condition is true then outer loop iterates again.Repeats until the condition becomes false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
#include <stdio.h> #include <conio.h> int main() { int i,j,n,k; char ch; printf("Enter number of rows: "); scanf("%d%c",&n,&ch); printf("Enter the symbol: "); ch=getchar(); i=1; k=n*2-1; do { if(i==1 || i==n || i==k) { j=1; do { if(j==1 || j==n) printf(" "); else printf("%c",ch); j++; }while(j<=n); } else { j=1; do { if(j==1 || j==n) printf("%c",ch); else printf(" "); j++; }while(j<=n); } printf("\n"); i++; }while(i<=k); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 |
Enter number of rows: 5 Enter the symbol: * *** * * * * * * *** * * * * * * *** |