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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 27

Thread: Print out a triangle by character *

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Print out a triangle by character *

    I need a triangle like this



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


    I just know how to print like this :

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

    Can you guys give me some hints. Thank you very much.
    My code is:
    	public static void main(String[] args) {
    		Scanner s = new Scanner(System.in);
    		System.out.print("Enter your number: ");
    		int n = s.nextInt();
    		for (int i = 0; i < n; i++) {	
    			for (int j = 0; j < n; j++) {
    				System.out.print("*");
    			}
    			System.out.println();
    		}
    	}
    Last edited by Genky; August 19th, 2012 at 07:52 AM.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Print out a triangle by character *

    Does this mean you have to input an odd number?

    You would have to print spaces on every line except the last line. How many spaces on the first line? How many on the second? You can get this value with some math and an index variable from one of the loops.

    Plan out what you have to make happen and then write the code to do it.

  3. #3
    Junior Member mysTTerE's Avatar
    Join Date
    Aug 2012
    Location
    India
    Posts
    2
    My Mood
    Depressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Print out a triangle by character *

    Quote Originally Posted by Genky View Post
    I need a triangle like this

    *
    ***
    *****
    *******
    ...code removed by moderator
    Last edited by copeg; August 18th, 2012 at 10:47 AM.

    "Stay Hungry Stay Foolish"
    --- Steve Jobs(1955-2011)

    "The quieter you become the more you are able to hear"
    --- Baba Ram Das

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Print out a triangle by character *

    Please don't spoonfeed code. Read this:
    http://www.javaprogrammingforums.com...n-feeding.html
    Last edited by Norm; August 19th, 2012 at 07:13 AM. Reason: spelling
    If you don't understand my answer, don't ignore it, ask a question.

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

    mysTTerE (August 18th, 2012)

  6. #5
    Junior Member
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Print out a triangle by character *

    I mean I was required to show a isoceles triangle. But I don't know how to edit it in above post.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Print out a triangle by character *

    What is the pattern of what is printed on each line?
    Take a piece of paper and draw on it the spaces and *s that you want to print.
    What prints on the first line? X spaces and one *
    What prints on the 2nd line? Y spaces, one *, Z space(s), one *
    Continue writing what goes on the next 3-4 lines.
    Now look at the number of leading spaces (X and Y above) and the number of inside spaces (Z) for the lines
    and see how they are related to the location of the line.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Print out a triangle by character *

    I saw there are 4 lines. Each line the number of spaces decrease by one, and the number of * increase 1,3,5,7.

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Print out a triangle by character *

    Can you compute the number of *s from the number of the line? Or another way would be to use a counter for the number of *s. Start at 1 and increase by 2 for each line.
    Look at using loops, one to print the spaces and one to print the *
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Print out a triangle by character *

                                   for (int i = 0; i <4; i++) {
    				System.out.printf("%" + i + "s", "");
    				for (int j = 0; j < 4; j++) {
    					System.out.print("*");
    				}
    				System.out.println();
    			}

    But I don't know how to print spaces. Why do I feel it's so hard .
    Last edited by Genky; August 19th, 2012 at 07:54 AM.

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Print out a triangle by character *

    how to print spaces
    A space is just another character. Put it inside of some "s:
    " " for a space
    vs
    "*" for an *
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Junior Member
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Print out a triangle by character *

    You mean like this
                              for (int i = 0; i <4; i++) {
    				System.out.print(" ");
    				for (int j = 0; j < 4; j++) {
    					System.out.print("*");
    				}
    				System.out.println();
    			}
    But it just print:
    ****
    ****
    ****
    ****

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Print out a triangle by character *

    Look at the steps in the algorithm you worked out for the code and get that right first.
    Then write the code. Your code obviously doesn't follow the right algorithm.
    Can you post the algorithm's steps so we can see how it is supposed to work?
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:

    Genky (August 19th, 2012)

  15. #13
    Junior Member
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Print out a triangle by character *

    -The first line it prints one *, spaces = number row - number of *
    - Each line decrease number of * by 2.

  16. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Print out a triangle by character *

    Not a good algorithm.
    The first line it prints one *
    I thought there were spaces before the * on the first line? You steps say to print an * first.
    Each line decrease number of * by 2.
    If you start with 1 and decrease by 2 there are negative numbers.
    What about the leading spaces on each line before the *s?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #15
    Junior Member
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Print out a triangle by character *

    Quote Originally Posted by Norm View Post
    Not a good algorithm.

    I thought there were spaces before the * on the first line? You steps say to print an * first.

    If you start with 1 and decrease by 2 there are negative numbers.
    What about the leading spaces on each line before the *s?
    You're so patient.


    The first line has 3 spaces, one *
    The second line has 2 spaces, three ***
    The second line has 1 spaces, five *****
    The second line has 0 spaces, three *******

    So, We have to print 4 row,spaces start each row -1, each increase number of * = 2.

  18. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Print out a triangle by character *

    write an expression using the line number to compute the number of spaces
    and another expression to compute the number of *s on a line
    The pseudo code could be:
    init variables
    begin loop1 for number of lines to print
    compute number of spaces for this line
    print the spaces
    compute the number of *s for this line
    print the *s
    move to next line
    end loop1
    If you don't understand my answer, don't ignore it, ask a question.

  19. #17
    Junior Member
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Print out a triangle by character *

    Quote Originally Posted by Norm View Post
    write an expression using the line number to compute the number of spaces
    and another expression to compute the number of *s on a line
    The pseudo code could be:

    	int n = 4;
    		for(int i =0; i<n;i++){
    			int space = n-1;
    			System.out.printf("%",spa," ");//want to print number of spaces
    				int numsta =i+2;//but it's not true
    				for (int j = 0; j < numsta; j++) {
    					System.out.print("*");
    				}
    				System.out.println();
    		}

  20. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Print out a triangle by character *

    Does the code work? Does it compile, execute and produce the desired results?
    If you don't understand my answer, don't ignore it, ask a question.

  21. #19
    Junior Member
    Join Date
    Aug 2012
    Location
    Turkey
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Think this way,

    int j=10;
    for(int i=1 ; i<10 ; i= i+2) {
    ...
    }

    i will give you *,
    j-i will give you white space

  22. #20
    Junior Member
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Print out a triangle by character *

    Quote Originally Posted by Norm View Post
    Does the code work? Does it compile, execute and produce the desired results?
    It didn't run in this line System.out.printf("%",spa," "); . Can you correct for me?
    And int numsta =i+2 does not bring correct result.

  23. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Print out a triangle by character *

    It didn't run in this line
    If you get a compiler error, please copy and paste the full text of the error message here. Otherwise explain what you mean by "it didn't run".
    And int numsta =i+2 does not bring correct result.
    Please explain what the problem is. Show the results that the program generates and explain what is wrong with it.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #22
    Junior Member
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Print out a triangle by character *

    Quote Originally Posted by Norm View Post
    If you get a compiler error, please copy and paste the full text of the error message here. Otherwise explain what you mean by "it didn't run".
    I searched and wrote code for print space like this:

    String space = String.format("%" + (n-1) + "s"," ");

    But it just prints 4 space. I don't know why.


    Please explain what the problem is. Show the results that the program generates and explain what is wrong with it.

    I increase each line print *+2 but it wasn't true.

  25. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Print out a triangle by character *

    I increase each line print *+2 but it wasn't true.
    Can you show the programs output and explain what the problem is.

    I don't understand what your question is. What is *+2?

    But it just prints 4 space. I don't know why.
    Does the value of n change? If it doesn't change then the printf() method call will print the same thing every time.
    If you don't understand my answer, don't ignore it, ask a question.

  26. #24
    Junior Member
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Print out a triangle by character *

    Quote Originally Posted by Norm View Post
    Can you show the programs output and explain what the problem is.

    I don't understand what your question is. What is *+2?


    Does the value of n change? If it doesn't change then the printf() method call will print the same thing every time.






    This is the out put:

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

    The problems is : The space doesn't show correct.

    I mean I've increased number of * each time in for loop = * + 2*.


    	int n = 4;
    		for(int i =0; i<n;i++){
    			String space = String.format("%" + (n-1) + "s"," ");
    			System.out.printf(space);
    			int numsta = i+2;
    				for (int j = 0; j < numsta; j++) {
    					System.out.print("*");
    				}
    				System.out.println();
    		}

  27. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Print out a triangle by character *

    The space doesn't show correct.
    Please explain what is wrong with the spaces.

    increased number of * each time in for loop = * + 2*.
    Does that give you the right output?
    Last edited by Norm; August 19th, 2012 at 12:22 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  28. The Following User Says Thank You to Norm For This Useful Post:

    Genky (August 20th, 2012)

Page 1 of 2 12 LastLast

Similar Threads

  1. help with loop to print an equliateral asterisk triangle
    By everyone0 in forum Loops & Control Statements
    Replies: 13
    Last Post: October 11th, 2012, 12:37 PM
  2. xfa.host.print: print a page + some page range.
    By gammaman in forum Totally Off Topic
    Replies: 2
    Last Post: May 10th, 2012, 08:07 AM
  3. [HELP] TRIANGLE!
    By kramista in forum Loops & Control Statements
    Replies: 10
    Last Post: July 29th, 2010, 12:58 PM
  4. ASCII Triangle
    By physics in forum Loops & Control Statements
    Replies: 1
    Last Post: March 27th, 2010, 06:39 AM
  5. The character '' is an invalid XML character exception getting
    By sumanta in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 9th, 2010, 12:13 PM