Hello I'm new here, I try this site when I found the url in the web when I am trying to solve my problem. And I became interested and join.

Hope you can help me with this one:

I have this program:

#include <stdio.h>


main()


{


char input;
int base,a,b,c,d,e;



printf("Input:");
scanf("%c", &input);
printf("Base:");
scanf("%i", &base);



e=base;


for (a=0;a<=base-1;a++)


{


for (d=0;d<=e-a;d++)


printf (" ");
for (b=0;b<=a;b++)


{


printf("%c", input);



}


printf("\n");
}


}

when I run this the result will be a triangle made of a inputed letter.

Input:A
Base:5
A
AA
AAA
AAAA
AAAAA
AAAAAA
Press any key to continue

the problem is I want the input to look like a equilateral triangle but the result is a right triangle.

There's something wrong with the second for statement but until now I don't know how to fix it.

I try changing the condition d<=e-a at the second for statement to d<=50-a but still it doesn't work.

what shall I do...

by the way I'm new with c programming.

(about the posting I am still studying how it works, so for the meantime I just copy and paste my codes... sorry for that :p)

Dani AI

Generated

The triangle is a centering problem plus an off‑by‑one in the loop bounds. ’s original loops print 1, 2, 3, ... characters (a right triangle) because the inner loop uses the row index directly and the space loop uses inclusive bounds that produce the wrong count. An equilateral (visually centered) triangle printed in a monospace console needs odd numbers of characters per row and the correct number of leading spaces.

Correct rule to implement (row numbers 1..n):

  • leading spaces = n - row
  • characters on the row = 2*row - 1

Put simply: for each row from 1 to base, print base-row spaces, then print 2*row-1 copies of the chosen character, then a newline. Using 1-based row indexing avoids many off‑by‑one mistakes; if 0‑based indexing is kept, adjust the formulas accordingly.

Practical C tips not yet covered in the thread:

  • Prefer int main(void) and return 0; for a well-formed program.
  • Use scanf(" %c", &ch) (note leading space) when reading a character if there may be leftover newlines.
  • For single-character output, putchar() is cheaper than printf.
  • To print the leading spaces in one call, printf("%*s", width, "") is a handy idiom instead of an explicit loop.
  • Watch <= vs < in loop conditions; inclusive <= is often what causes an extra iteration.

’s posted solution follows the same base-row / 2*row-1 idea and is the right approach; the notes above explain why that pattern is correct and how to avoid the common pitfalls that produced the right triangle.

Recommended Answers

All 5 Replies

>(about the posting I am still studying how it works, so for the meantime I just copy and paste my codes... sorry for that :p)

What's so hard to understand?

#include <stdio.h>

main()

{

char input;
int base,a,b,c,d,e;


printf("Input:");
scanf("%c", &input);
printf("Base:");
scanf("%i", &base);


e=base;

for (a=0;a<=base-1;a++)

{

for (d=0;d<=e-a;d++)

printf (" ");
for (b=0;b<=a;b++)

{

printf("%c", input);


}

printf("\n");
}

}

Yes Ive done it hehe code, sorry for the inconvenience.

sorry for that. I had just gotten the trick for posting. sorry for the inconvenience..

sorry for that. I had just gotten the trick for posting. sorry for the inconvenience..

Futher homework for you: Formatting C code
A possible solution:

#include <stdio.h>

int main ( void ) 
{

    char input;
    int base,a,b;


    printf("Input:");
    scanf("%c", &input);
    printf("Base:");
    scanf("%i", &base);

    for ( a = 1; a <= base; a++)
    {
        for ( b = 0; b < base-a; b++ )
            putchar(' ');
        for ( b = 0; b < 2*a - 1; b++ ) 
            putchar( input );
        putchar( '\n' );
    }
    return 0;
}

Thank you and also the reading assignment helps enlightening me. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.