Solved

java triangle

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

    *
  **
***
and

***
 **
   *
0
Comment
Question by:pauledwardian
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example 11 59
tomcat administrtor 12 68
pagenation logic how it is working in my code 1 71
Find out all repos that a user is most active on Github 1 33
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

733 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