C Program to print the X star pattern – In this article, we will learn the multiple methods of how to print the X 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 methods discussed in this piece are as follows:
- Using For Loop
- Using While Loop
- Using Do-While Loop
An X star pattern is a pattern that you have to print using the “*” symbol in the form of an X. This is what it looks like:
In this case, the value of “n” has been set to 5. So, each arm of the X star pattern will have 5 “*” symbols.
Thus, the various methods used to print the X star pattern 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) The inner for loop iterates through columns with the structure for(j=1;j<=k;j++).
a) if j=i or j=k-i+1 condition is true then this loop prints symbol.
b) If the condition is false then this loop prints space.
5) 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 |
#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++) { for(j=1;j<=k;j++) { if(j==i || j==k-i+1) printf("%c",ch); 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) j=1, The inner while loop iterates through columns until the condition j<=k becomes false.
a) Prints symbol if j=i or j=k-i+1 the condition is true. Otherwise, it prints space.
b) j value increased by 1.
4) 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 |
#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; while(j<=k) { if(j==i || j==k-i+1) printf("%c",ch); 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) j=1, The inner do-while loop iterates through columns.
a) if j=i or j=k-i+1 the condition is true then loop prints symbol.
b) otherwise, it prints space.
c) j value increased by 1.
Checks the condition j<=k.If the condition is true then inner loop iterates again.Repeats until the condition becomes false.
4) The cursor comes to next line.
5) 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 |
#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 { j=1; do { if(j==i || j==k-i+1) printf("%c",ch); printf(" "); j++; }while(j<=k) ; 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: * * * * * * * * * * * * * * * * * * |