for loop

This is a discussion on for loop within the C Programming forums, part of the General Programming Boards category; Code: i made a source code lwhich has an output like this: * ** *** **** ***** and a source ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    for loop

    Code:
    i made a source code lwhich has an output like this:
    
    *
    **
    ***
    ****
    *****
     and a source code like this
    int i,j;
    
      for(i=1;i<=5;i++){
        for(j=1;j<=6-1;j++){
         puts('*');
      }
    
       printf("\n");
        }
    
    return 0;
    }
    
    how can i turn the five(5)columns on the right side
    and the opposite, the single * is at the bottom and a source code of * that forms an inverted triangle
    thank you
    Last edited by ricnoah; 03-16-2008 at 04:10 AM.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Run the loop once forwards, then run it again backwards.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    2
    so how is that?should i change the < to >?
    or the initial value of 'i' will be equal to to 5?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Quote Originally Posted by ricnoah View Post
    Code:
    i made a source code lwhich has an output like this:
    
    *
    **
    ***
    ****
    *****
     and a source code like this
    int i,j;
    
      for(i=1;i<=5;i++){
        for(j=1;j<=6-1;j++){
         puts('*');
      }
    
       printf("\n");
        }
    
    return 0;
    }
    
    how can i turn the five(5)columns on the right side
    and the opposite, the single * is at the bottom and a source code of * that forms an inverted triangle
    thank you
    That code will NOT produce the output you've indicated, it won't even compile cleanly (puts does not accept a single character argument). And don't put code tags around your entire post, just the code parts. If you were to understand how correct code to produce the first part worked then it should be easy enough to figure out how to do what you're asking.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21