simple triangle

This is a discussion on simple triangle within the C Programming forums, part of the General Programming Boards category; hi! now i need to build this triangle : Code: * ** *** **** imagine it's align to the right. ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    4

    simple triangle

    hi!
    now i need to build this triangle :
    Code:
       *
      **
     ***
    ****
    imagine it's align to the right.
    i have one solution-
    one major loop that controls the number of lines and two loops inside for managing the spaces and chars.

    can you give me another solution?

    thx!!
    Last edited by -Dizzy-; 11-07-2005 at 09:59 AM.

  2. #2
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Inside the loop for each line, instead of having two loops, one for '*' and one for ' ', think about how you could do those both in one loop with a conditional to print either '*' or ' ' depending on how far through the loop you are.
    hello, internet!

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    New Delhi
    Posts
    40
    can you give me another solution?
    Well, I have another solution:-

    Code:
    printf("*");
    printf("\n**");
    printf("\n***");
    printf("\n****");
    printf("\n*****");
    Yeah but since your probably doing that triangle thingie as an assigment, I would rather stick to your solution, unless ofcouse you want to freak your prof. out.
    Thanks,
    Sahil

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    4
    got it done!!
    moi, your advice really helped... thx!
    Code:
    for (i=1;i<=4;i++)
    {
    	for (j=1;j<=4;j++)
    		if (j>4-i)
    			printf("*");
    		else printf(" ");
    	printf("\n");
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Resizing a triangle. Why is my code not working?
    By gozu in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2007, 06:40 PM
  2. Stupid Logic Problem Need Outside Viewpoint
    By RP319 in forum C++ Programming
    Replies: 5
    Last Post: 03-03-2005, 10:59 PM
  3. Triangle
    By BB18 in forum C Programming
    Replies: 14
    Last Post: 10-16-2004, 11:59 PM
  4. Just in case: "Odd" Triangle Challenge (for me)
    By BB18 in forum C Programming
    Replies: 3
    Last Post: 10-09-2004, 01:02 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM

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