Solved

java triangle

Posted on 2011-03-01
5
738 Views
Last Modified: 2012-05-11
how is it possible to draw this type of triangle in java:

    *
  **
***
and

***
 **
   *
0
Comment
Question by:pauledwardian
5 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35014431
What do you mean by that question?
You cat just print like that:

System.out.println("  *");
System.out.println(" **");
System.out.println("***");

or you can create a panle and draw them and fill them in.

Please, explain what do you mean by this question?

0
 
LVL 92

Expert Comment

by:objects
ID: 35014444
0
 

Author Comment

by:pauledwardian
ID: 35014481
can you do these two triangles  by for loops please?

thanks,
edward
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 35014812
put for loop !
and use system.out.print

for each(increment) it will print one *

put if  inside the loop
system.out.println

0
 
LVL 3

Accepted Solution

by:
rmonsen earned 500 total points
ID: 35015854
		int trisize = 3;

		// Draw first triangle
		for (int i = 1; i <= trisize; i++) {
			// Spaces infront
			for(int j = 0; j < trisize - i; j++ ) {
				System.out.print(" ");
			}
			// stars
			for(int j = trisize; j > trisize - i; j-- ) {
				System.out.print("*");
			}
			System.out.println();
		}

		// Draw second triangle
		for (int i = trisize; i >= 1; i--) {
			// Spaces infront
			for(int j = 0; j < trisize - i; j++ ) {
				System.out.print(" ");
			}
			// stars
			for(int j = trisize; j > trisize - i; j-- ) {
				System.out.print("*");
			}
			System.out.println();
		}


		// Draw both in one loop
		for (int i = trisize; i >= -trisize; i--) {
			int stars = 1 + trisize - Math.abs(i);
			if (i == 0) continue; // Skip iteration
			
			for(int j = 0; j < trisize - stars; j++ ) {
				System.out.print(" ");
			}
			// stars
			for(int j = trisize; j > trisize - stars; j-- ) {
				System.out.print("*");
			}
			System.out.println();
		}

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 107
Java Loop 6 61
Apps blocked by Java 9 79
Glassfish admin console not working 1 12
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question