Thread: problem with the program

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    13

    problem with the program

    Code:
    #include<stdio.h>
    main()
    {
        int i,j;
    for(i=0;i<5;i++)
        printf("\n");
        for(j=0;j<5;j++)
          {
               if(i>=j)
               printf("* ");
               else
               printf(" ");
          }
    
    }
    i want to get output as
    *
    **
    ***
    ****
    *****

    but the above code did not work....please help me where it went wrong.....

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    There is no need of the else statement and you have misplaced the printf("\n"); statement. It should be at the end of the first for loop.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you do not need if at all, just make the inner loop
    Code:
    for(j=0;j<=i;j++)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    A couple of issues with your program are the following:

    1. Never use simple main. Use int main(void) and make sure you return 0 at the end of the program.

    2. With the above program there are a lot of problems.

    [insert]
    Code:
    #include<stdio.h>
    main()
    {
        int i,j;
    for(i=0;i<5;i++)
        printf("\n");
        for(j=0;j<5;j++)
          {
               if(i>=j)
               printf("* ");
               else
               printf(" ");
          }
    
    }
    Your first for loop is not having any {}. So its scope is just to the next line. And instead of the second for loop do the following

    So it should be like this

    [insert]
    Code:
    for(i=0;i<5;i++)
    	{
        printf("\n");
        for(j=0;j<= i;j++)
          {
               printf("* ");
          }
    	}

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    thanks a lottt to u all.........now its working......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 11:10 AM
  2. Program Termination Problem
    By dacbo in forum C Programming
    Replies: 3
    Last Post: 01-23-2006, 02:34 AM
  3. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  4. Replies: 20
    Last Post: 06-12-2005, 11:53 PM