Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Need Hint for Triangle???

  1. #1
    Member Ganeprog's Avatar
    Join Date
    Jan 2014
    Location
    Chennai,India
    Posts
    80
    My Mood
    Love
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Question Need Hint for Triangle???

    Hi,

    I need to build the triangle like below.Please give hint to solve this using loops.

                     *
                    * *
                   * * *
                  * * * *
                 * * * * *

    thanks in advance.
    Last edited by Ganeprog; January 16th, 2014 at 09:36 AM. Reason: triangle shape changed


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Need Hint for Triangle???

    Hi, read this Announcements - What's Wrong With My Code? to find out how to post formatted code and to preserve formatting.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Need Hint for Triangle???

    Then show what you've tried. Ask specific questions, post sample runs, errors, etc. Then we can give hints.

  4. #4
    Member Ganeprog's Avatar
    Join Date
    Jan 2014
    Location
    Chennai,India
    Posts
    80
    My Mood
    Love
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: Need Hint for Triangle???

    I tried but i dont know how to start this. give some hint or idea to solve this above triangle or Please provide some initiation point to start

  5. #5
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Need Hint for Triangle???

    Quote Originally Posted by Ganeprog View Post
    I tried but i dont know how to start this. give some hint or idea to solve this above triangle or Please provide some initiation point to start
    First, I imagine you want (or you have been asked to do) to have a variable that contains the "height" of the triangle, e.g.:

    int height = 5;

    And given only this value, generate dynamically the triangle, so that changing this value you get another correct triangle.

    You need a main loop (for) cycle for each row, I think this is quite trivial. For each row you need to do another loop, some prints but also few calculations.
    If you want to have a fixed number of spaces at the beginning of each line, it's ok, it's a simple fixed println. Then you need a number of spaces that depends on the current row. On the first row, how many spaces do you need before the single "*" in order to have this centered?
    And you need a space between two consecutive "*".

    So please, start from these considerations.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  6. #6
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Need Hint for Triangle???

    public class MyTriangle{
        public static void main(String args[]){
            MyTrinagle mt = new MyTriangle();
            // up to you ....
        }
    }

Similar Threads

  1. [SOLVED] For Loop - need a hint for what to do next
    By tufaylahmed in forum Loops & Control Statements
    Replies: 6
    Last Post: November 19th, 2012, 01:06 AM
  2. Java application Hint
    By Ahmedz in forum Java Theory & Questions
    Replies: 2
    Last Post: August 26th, 2012, 12:46 PM
  3. Homework help, don't understand teach's hint, need some direction
    By crazed8s in forum Java Theory & Questions
    Replies: 2
    Last Post: December 8th, 2010, 04:56 PM
  4. [HELP] TRIANGLE!
    By kramista in forum Loops & Control Statements
    Replies: 10
    Last Post: July 29th, 2010, 12:58 PM
  5. Triangle Question
    By Leeds_Champion in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 29th, 2009, 11:33 PM