This is very simple.
What we want is a mathematical function which will do the following
0 -> 1
1 -> 3
2 -> 5
3 -> 3
4 -> 1
If we break this down we get the equation
y = 5 - abs(x - 2) * 2
So, our inner for loop needs to implement that function, while our outer for loop goes from 0-4 (inclusive)
#include <stdio.h>
int abs(int n) {
return n > 0 ? n : -n;
}
int main() {
for (int i = 0; i < 5; ++i) {
int max = 5 - abs(i - 2) * 2; for (int j = 0; j < max; ++j) {
putchar('*');
}
putchar('\n'); }
return 0;
}