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 10 of 10

Thread: Half Triangle printout

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Half Triangle printout

    Hey guys,I need to Write a program like say I input two specifications i.e. 5 & 5 it then prints out like half a triangle. Prints out 5 down & 5 across increasing by one on each new line as seen in example of *'s. Any help will be appreciated thank you.

    :Example:
    java Lcode 5 5 (inputs)
    *
    **
    ***
    ****
    *****


    My Code so far is only printing 5 down which Im struggling to make any progress.
    public class Lcode
    {
    	public static void main(String args[])
    	{
    	int across = Integer.parseInt(args[0]), // For taking inputs across
    	int	down = Integer.parseInt(args[1]); // For taking inputs	down
     
    	for(int i = 0; i < down; i++)
    				{
    				System.out.println("*");
    				}			
    	}
     
    }
    Last edited by jps; July 28th, 2013 at 08:28 AM. Reason: code tags


  2. #2
    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: Half Triangle printout

    First line prints one star
    Second line prints two stars
    Third line prints three stars
    Fourth line prints ? stars

    See a pattern? Once you see the pattern, how might you program it? There are different ways, but one is to use two for loops, one nested inside the other. The outside loop keeps track of the line being printed and the inside loop the number of stars for each line.

    See if you can modify your existing code to do that. When you post your updated code, please post it in code tags.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    Mr_Nice_Guy (July 29th, 2013)

  4. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Half Triangle printout

    What should the output look like if the input is 3 7?
    Improving the world one idiot at a time!

  5. The Following User Says Thank You to Junky For This Useful Post:

    Mr_Nice_Guy (July 29th, 2013)

  6. #4
    Junior Member
    Join Date
    Jul 2013
    Location
    uk
    Posts
    15
    Thanks
    2
    Thanked 3 Times in 3 Posts

    Default Re: Half Triangle printout

    hello,
    for rows and columns output you would use nested loops. this is an array-like-value resulting from your code so the for loop you have written is good one step towards your program however providing the next loop should have more thinking in handling the out put. you may need extra mind workout for this, keywords: you would use while loop and check if certain index at the desired value? so you would break the loop and shift the output to the next line? have a brain exercise on that. cheers

  7. The Following User Says Thank You to Alaa For This Useful Post:

    Mr_Nice_Guy (July 29th, 2013)

  8. #5
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Half Triangle printout

    your required code works with single variable as input as shown below, hope helpful for u
    ///
    ...code removed
    Last edited by copeg; July 29th, 2013 at 06:55 PM. Reason: Removed Spoonfeeding

  9. The Following User Says Thank You to suman.marri For This Useful Post:

    Mr_Nice_Guy (July 29th, 2013)

  10. #6
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Half Triangle printout

    Hey Guys
    Thanks for the Replies,
    I'm going to work on it now

  11. #7
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Half Triangle printout

    Write a program that draws a right angle triangle, using characters, with dimensions given as
    command line arguments. If you're clever, you can use dierent characters to reduce the \jaggies" on the sloping edge. Examples:

    $ java TriAlias 5 5
    O
    OO
    OOO
    OOOO
    OOOOO

    java TriAlias 60 5
    OOOOooooooo...
    OOOOOOOOOOOOOOOOOOooooooo...
    OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOooooooo...
    OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOoooo ooo...
    OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO OOOOOOOOOOooooooo...

    Here is what iv Reattempted. Its printing rows and columns but not in pattern, not sure how to implement this through the code iv got so far. Im such a noob.

    MY REAMMENDED Code

    public class KudoTry{
    public static void main (String[] args){

    int row = Integer.parseInt(args[0]),
    col = Integer.parseInt(args[1]);

    for(int r = 0; r < row; r++){
    for(int c = 0; c < col; c++){
    System.out.print("*");
    }
    System.out.println();
    }


    }
    }

  12. #8
    Junior Member
    Join Date
    Jul 2013
    Location
    uk
    Posts
    15
    Thanks
    2
    Thanked 3 Times in 3 Posts

    Default Re: Half Triangle printout

    hello,
    try this code and see what you get. however trying class 9 7 will output different than class 7 9
    have a look...
    ....
    Last edited by copeg; July 29th, 2013 at 06:55 PM. Reason: Spoonfeeding removed

  13. The Following User Says Thank You to Alaa For This Useful Post:

    Mr_Nice_Guy (July 29th, 2013)

  14. #9
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Half Triangle printout

    hieee i am just posting this reply so that you can review your code , as it is not working as required mentioned above. when a user enter arguments as 1, 2 or 1,3 or 1,4 or 1,5 or........ 1,100 the output is single line. when a user enter arguments as 2,1 or 2, 2 or 2,3 or 2,4 or 2,5 or........ 2,100 the output is double line. and the same with 3,1 or 3,2 or 3,3.
    The above output of your program can be printed even using the single argument as i posted the code earlier. Thank you

  15. The Following User Says Thank You to suman.marri For This Useful Post:

    Mr_Nice_Guy (July 29th, 2013)

  16. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Half Triangle printout

    Something about triangle printing and spoonfeeding - seem to go hand in hand.

    Everyone - especially Alaa and suman.marri, please read:
    http://www.javaprogrammingforums.com...n-feeding.html
    Any further spoonfeeding in this thread will be deleted. Past spoonfed code may be deleted. EDIT: past spoonfed code has been deleted

  17. The Following User Says Thank You to copeg For This Useful Post:

    Mr_Nice_Guy (July 29th, 2013)

Similar Threads

  1. JMenuBar disappears half the time when using a certain line of code?
    By !Marcus in forum Java Theory & Questions
    Replies: 2
    Last Post: December 26th, 2011, 04:05 PM
  2. Square Game (a half solved, please help with the rest!!!)
    By LadyBelka in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 23rd, 2011, 04:42 PM
  3. wanting to convert printout to UPPER case
    By welikedogs in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 3rd, 2010, 01:22 PM
  4. loop and a half
    By Brain_Child in forum Loops & Control Statements
    Replies: 2
    Last Post: January 22nd, 2010, 03:52 AM