+ 2
So what am i doing wrong in the following pattern?
pattern:. A BAB CBABC DCBABCD https://code.sololearn.com/cZNC8KOHGsnP/?ref=app
5 Réponses
+ 5
That's the most basic and inefficient way to draw a triangle. You should take your time and improve it.
Basically, first and forth for loops (inside the outer loop) are responsible for space padding on both sides of the triangle. Second and third loops do necessary addition and subtraction (based on the current value of the outer loop index) to make up the letter sequence — one loop generate letters from left to the middle and other from middle + 1 to the right.
+ 3
Possible Fix:
#include <stdio.h>
int main() {
char a, b, k;
int i, j;
printf("enter a character:");
scanf("%c", &a);
b = a;
for (i = 5; i >= 1; i--)
{
for (j = i - 1; j >= 1; j--)
printf(" ");
for (k = (a + 5 - i) ; k >= b; k--)
printf("%c", k);
for (k = a + 1; k <= (b + 5 - i); k++)
printf("%c", k);
for (j = i - 1; j >= 1; j--)
printf(" ");
printf("\n");
}
}
Sample output:
enter a character: A
A
BAB
CBABC
DCBABCD
EDCBABCDE
+ 1
ok that's confusing me ,you added two for statements for space and instead of increasing a,you added 5!
0
If I am thinking right!
0
ok I did some changes and its returning no output ,why?