#include<conio.h>
#include<stdio.h>

void main()
{
 int iRows;
 clrscr();

 printf("Enter a number :");
 scanf("%d",&iRows);
 if(iRows>0)
  {
   for(int iResult=0,iCount=0,iRows; iResult<=iRows; iResult++)
      for(iResult=1,iCount=0,iRows; iCount<=iResult; iCount++)
     printf("*");
      printf("\n");
  }//for
 getch();
}//main

please help me to correct my source code: i should make a program like this:
*
**
***
****
*****
.
.
.
n
n is any positive number inputted by the user.

Recommended Answers

All 7 Replies

You keep resetting iResult in the inner loop when it shouldn't be modified. This is correct:

int iResult, iCount;

for(iResult=0; iResult<=iRows; iResult++) {
    for(iCount=0; iCount<=iResult; iCount++)
        printf("*");

    printf("\n");
}

Also notice that I added braces around the outer loop. While you can omit the braces if the entire body of the loop consists of only one statement, your outer loop contains two: the inner loop is one statement and the printf() call is a second. Failing to use braces here means that newlines won't be displayed at the correct time and you'll just have one long line of asterisks.

I recommend that beginners always use braces to avoid this kind of error.

ohh i see thank you for that!

Member Avatar for I_m_rude

@ronnel he(James sir) is solution of every problem of yours ;)

last question sir i think my condition for the inner loop is wrong? can you suggest another one? thank you!

i think my condition for the inner loop is wrong?

Why do you think it's wrong?

because it should show:
*
**
***
****
*****
.....................
but it shows just
***********

I'm guessing you didn't comprehend the entirety of my post. I addressed that problem by talking about braces and even gave you a snippet of the correct code. Here's a complete working program, since you seem to have trouble cutting and pasting snippets into existing code:

#include "stdio.h"

int main(void)
{
    int iRows;

    printf("Enter a number :");
    fflush(stdout);
    scanf("%d",&iRows);

    if(iRows>0)
    {
        int iResult, iCount;

        for(iResult=0; iResult<=iRows; iResult++) {
            for(iCount=0; iCount<=iResult; iCount++)
                printf("*");

            printf("\n");
        }
    }

    return 0;
}
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.