I’m trying to create a pyramid in C, i need to get the next pattern:
A
ABA
ABCBA
ABCDCDA
ABCDEDCBA
I need to use nested for loops, but the decrementing is not working properly for me, this is my code:
int main(void)
{
int i, j, k, g;
char userLatter;
printf("please enter an uppercase letter:\n");
scanf("%c", &userLatter);
int asci = userLatter;
for (i = 0; i < 5; i++)
{
for (j = 4; j > i; j--)
{
printf(" ");
}
asci = userLatter;
for (k = 0; k <= i ; k++)
{
printf("%c", asci++);
}
for (g = 1; g <= i; g++)
{
printf("%c", --asci);
}
printf("\n");
}
}
this is the input “a”
and the output im getting is:
a
abb
abccb
abcddcb
abcdeedcb
Can you please tell me what am I doing wrong?
tnx
Add a:
between the two
forloops.In the first loop,
asci++performs a postfix increment: the expression is evaluated toasciand then the object is decremented.