C program to print a half diamond star pattern – In this article, we will brief in on the several methods used to print a half diamond 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 ways used to do so in this specific article are as follows:
- Using For Loop
- Using While Loop
- Using Do-While Loop
As we all know, a diamond is a very popular shape that is generally used in daily life, unlike the actual thing which is extremely expensive.
This is how a half diamond star pattern looks like:
As you can see, firstly, you need to enter the number of rows to print the diamond star.
Then, the number that you have entered, those many rows will be printed on both sides of the middle.
Thus, the ways to print a half diamond star pattern are as follows:
Using For Loop
- Read the no.of rows, symbol and store the rows number into n, symbol into ch.
2) The 1st for loop iterates through rows with the structure for(i=1;i<=n;i++).
a) The 2nd for loop iterates through columns with the structure for(j=1;j<=i;j++).
It prints symbol.
b) Cursor comes to next line after each iteration of the 1st for the loop.
An upper part of the half diamond pattern will be formed after all iterations of above for loops.
3) The 3rd for loop iterates through rows with the structure for(i=n-1;i>0;i–).
a) The 4th for loop iterates through columns with the structure for(j=1;j<=i;j++)
It prints symbol.
b) Cursor comes to next line for each iteration of the 3rd for the loop.
A lower part of the half diamond will be formed after all iterations of 3rd for loop, 4th for the 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 |
#include <stdio.h> #include <conio.h> int main() { int i,j,n; char ch; printf("Enter number of rows: "); scanf("%d%c",&n,&ch); printf("Enter the symbol: "); ch=getchar(); for(i=1;i<=n;i++) { for(j=1;j<=i;j++) { printf("%c",ch); } printf("\n"); } for(i=n-1;i>0;i--) { for(j=1;j<=i;j++) { printf("%c",ch); } 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
- The 1st while loop iterates through rows until the condition i<=n becomes false.
a)The 2nd while loop iterates through columns until the condition j<=i becomes false.
It prints symbol.j value increased by 1.2nd while loop again checks the condition.
b) Cursor comes to next line.i value increased by 1. The 1st while loop again checks the condition.
2,3 steps represent the upper part of the half diamond star pattern.
2) i=n-1, The 3rd while loop iterates through rows until the condition i>0 becomes false.
a) The 4th while loop iterates through columns until the condition j<=i becomes false.
It prints symbol.j value increased by 1. The 4th while loop again checks the condition.
b) Cursor comes to next line. i value decreased by 1. The 3rd while loop again checks the condition.
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 |
#include <stdio.h> #include <conio.h> int main() { int i,j,n; char ch; printf("Enter number of rows: "); scanf("%d%c",&n,&ch); printf("Enter the symbol: "); ch=getchar(); i=1; while(i<=n) { j=1; while(j++<=i) { printf("%c",ch); } printf("\n"); i++; } i=n-1; while(i>0) { j=1; while(j++<=i) { printf("%c",ch); } 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
- For half part of the half diamond star pattern.
2) The 1st do-while loop iterates through rows.
a) The 2nd do-while loop iterates through columns.
It prints symbol.j value increased by 1.
Checks the condition j<=i. If condition true then loop iterates again. Repeats until the condition becomes false.
b) Cursor comes to next line.i value increased by 1.
c) Checks the condition i<=n. If the condition is true then the loop iterates again. Repeats until the condition becomes false.
For below half part of the half diamond star pattern.
3)i=n-1, The 3rd do-while loop iterates through rows.
a) j=1, The 4th do-while loop iterates through columns.
It prints symbol. j value increased by 1.
Checks the condition j<=i. If the condition is true then loop iterates again. Repeats until the condition becomes false.
b) Cursor comes to next line.i value decreased by 1.
c) Checks the condition i>0. If the condition is true, 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 |
#include <conio.h> #include <stdio.h> int main() { int i,j,n; char ch; printf("Enter number of rows: "); scanf("%d%c",&n,&ch); printf("Enter the symbol: "); ch=getchar(); i=1; do { j=1; do { printf("%c",ch); }while(++j<=i); printf("\n"); } while(++i<=n); i=n-1; do { j=1; do { printf("%c",ch); }while(++j<=i); printf("\n"); } while(--i>0); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 |
Enter number of rows: 5 Enter the symbol: . . .. ... .... ..... .... ... .. . |