Does it give:
#include <stdio.h>
int main() {
for (int x = 9; x > 0; x--) {
for(int y = 9 - x; y > 0; y--) printf(" ");
for (int y = 1; y <= x * 2 - 1; y++) printf("*");
printf("\n");
}
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
In addition to eliminating one of the ties that only repeated what the other did, I gave an organized and modernized in the code. I preferred to invert the loop count and use easier to read variable name.
Do not use conio.h
. Is obsolete.
But if you want to make it simpler and use a loop, you can do this:
int main(void) {
for (int x = 0; x < 1; x++) {
printf("*****************\n");
printf(" ***************\n");
printf(" *************\n");
printf(" ***********\n");
printf(" *********\n");
printf(" *******\n");
printf(" *****\n");
printf(" ***\n");
printf(" *\n");
}
}
When less deviations and variables and changes of state, simpler, even if it is not the shortest.
If you still want the shortest you have a few tricks possible. An option:
#include <stdio.h>
int main(void) { for (int x = 9; x > 0; x--) printf("%.*s%.*s\n", 9 - x, " ", x * 2 - 1, "*****************"); }
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
by the way, avoid having variables with the name
l
- is a symbol difficult to distinguish visually from1
and makes reading your code harder. (In the case of the source here to stackoverflow, the symbol is virtually identical to1
- but even with other sources is pretty bad)– jsbueno