# ASCII Triangle

• March 11th, 2010, 05:44 PM
physics
ASCII Triangle
Hi,
I need to create a triangle based on a height value given by the user.
The base of the triangle is calculated with:
Code :

```private int getTriangleWidth( int triangleHeight ) { return 2 * triangleHeight - 1; }```
and the method I am trying to use the make the triangle is:
Code :

```private void printTriangle( int height, int margin, boolean pointUp ) { int base = this.getTriangleWidth( height ); int m; int n; int o;   if( pointUp ) {   for( n = 0; n < height; n++ ) { for( o = -1; o < n; o++ ) { System.out.print( "*" ); } System.out.println( " " );   } } }```
If the user inputs 5, this results with a triangle that looks like this (I haven't included my driver method that actually tests the output):
*
**
***
****
*****

but I need an equilateral triangle: if the user inputs 5, it should have a base of 9 and a height of 5.
I understand that in my code above I don't have it depending on the base value at all but I'm not sure how to include it in or how to get the whitespace to come before the * in an even way so that I get my desired result.

Any hints would be wonderful
• March 27th, 2010, 06:39 AM
Sunil Raghuvanshi
Re: ASCII Triangle
Hi dear I have modified your code try this. It will work now

Code :

```public class Main { public static void main(String[] args) { Main obj=new Main(); obj.printTriangle(5); } private void printTriangle(int height) { int n; for( n = 0; n < height; n++ ) { for(int j=height;j>n;j--) System.out.print(" "); for(int i=0;i<=n*2;i++) System.out.print("*"); System.out.print("\n"); } } }```