Results 1 to 2 of 2
  1. #1
    blackbeltsas is offline Member
    Join Date
    Oct 2010
    Posts
    1
    Rep Power
    0

    Default Outputting a * triangle

    Hey I am new to forums, so if I don't put something in the post please tell me. I am trying to out put a "*" triangle from a user input of the height and base, but not just a regular right triangle. If the user put in a height of 3 and a base of 5 it should look like this
    *
    ***
    *****
    i just cant figure out the algorithm to use in a for loop. This is what I have for the for loop so far
    for(int i=0; i < triHeight;i++)
    {
    for(int j = 1; j<=triBase; j++)
    {
    System.out.printf("*");
    }
    System.out.println();
    }
    with triHeight and triBase being ints entered by the user. This prints out a square. I was told that the j increment should have something to do with the base and the height but i am really lost.

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default

    blackbeltsas,

    Firstly, welcome to the forums.

    The problem you are having relates to the second for loop. As the value of 'triBase' never changes between loops, the output for each repetition is always going to be the same.
    I'm not going to give you the code straight off, that would be too easy. Instead I can point you in the right direction.

    As already mentioned, the value in the second for loop needs to change between repetitions. But what value should this take?

    Just to get you started, divide 'triBase' by 'triHeight' this give you the increment per line.

    If you're still stuck then post back.

    Regards.

Similar Threads

  1. ASCII Triangle
    By physics in forum New To Java
    Replies: 2
    Last Post: 03-13-2010, 01:00 AM
  2. triangle program
    By computerbum in forum New To Java
    Replies: 8
    Last Post: 03-02-2010, 04:55 AM
  3. Triangle
    By jkswebsite in forum New To Java
    Replies: 8
    Last Post: 01-10-2009, 02:08 PM
  4. Is it a right triangle? (Code help)
    By TheApostle in forum New To Java
    Replies: 8
    Last Post: 10-07-2008, 08:39 PM
  5. Simple Addition Program Outputting Wrong Value
    By carlodelmundo in forum New To Java
    Replies: 4
    Last Post: 08-05-2008, 03:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •