Star Patterns in C
Two pyramids in two for loop
When using two for loops separately inside the main for loop to create this pyramid, it takes a lot of memory and time to compile. So it is efficient in every way to use only one for loop and an if statement inside the main for loop.
Output :
* ** *** **** *** ** *
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <stdio.h> int main(void) { int a, b, c; for (a = 1; a <= 9; a++) { for (b = 0; b < 5; b++) { if ((a < (4 + b)) && (a > (4 - b))) { printf("*"); } else { printf(" "); } } printf("\n"); } return 0; } |
Comments
Post a Comment