# creating a basic triangle using asterisks

Page 1 of 1

## 12 Replies - 32768 Views - Last Post: 11 April 2011 - 10:01 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=226981&amp;s=c87669e61bf881d734764f2b2d75fbc0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 newkid_onthe_block

Reputation: 0
• Posts: 9
• Joined: 13-March 11

# creating a basic triangle using asterisks

Posted 10 April 2011 - 02:34 PM

I am trying to design a program as practice for an upcoming exam I have. I am trying to make a basic triangle using asterisks. I am fairly new to Java and I'm not sure how to get started however, I know I need to use nested loops. I want to get user input for the size of the triangle. For example, if the user inputs 3, the triangle would look like this

*
**
***
**
*

I would appreciate any advice or tips.. Thank you!!

This is what I have so far its not much but I'll post below..

```/** This program takes a number from 1 to 50 from the user
* and output asterisks in the shape of a triangle with the
* user inputed number being the largest amount of asterisks*/

import java.util.Scanner;//Get Scanner from java utility

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

Scanner input = new Scanner(System.in);//Create Scanner object

int line; //declare the number of lines
int numA; // declare the size of the triangle
String ast = "*";

System.out.println("Enter the size of a triangle (1 to 50): ");
}
}

```

Is This A Good Question/Topic? 0

## Replies To: creating a basic triangle using asterisks

### #2 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: creating a basic triangle using asterisks

Posted 10 April 2011 - 02:48 PM

Sometimes, the triangle printed looks like:

```    *
***
*****
*******
*********

```

rather than what you've shown, but we can work with what you have.

Describe what has to be done in words:

"If the user inputs the number 5, I have to print rows of stars that begin with one star then add another star until the user's number is reached, then decrease the number of stars printed back to one."

How many rows is that? Count the number of stars to be printed: 1 star, 2 star, 3, 4, 5, 4, 3, 2, 1 = 9. That's one less than twice the number input by the user? Will that always be the case? Seems to make sense.

So all you have to do is create a loop that prints increasing numbers of stars until the user's number is reached and then prints rows of decreasing stars until 1 star is printed.

In primitive pseudo code:

```Get user's number, x
for 1 to x, x increasing by one
print a line of x stars
for x - 1 to 1, x decreasing by one
print a line of x stars

```

Can you code it now?

This post has been edited by GregBrannon: 10 April 2011 - 02:49 PM

### #3 newkid_onthe_block

Reputation: 0
• Posts: 9
• Joined: 13-March 11

## Re: creating a basic triangle using asterisks

Posted 10 April 2011 - 03:02 PM

Okay, that makes sense. How would you recommend I print increasing amount of stars until the user's inputted number? Should I use the asterisk as a String type?

### #4 softwareEngineer()

Reputation: -28
• Posts: 247
• Joined: 08-April 11

## Re: creating a basic triangle using asterisks

Posted 10 April 2011 - 03:06 PM

What do you mean by increasing amounts of stars until the user's?

Are you saying you want it to run until the user does something?

### #5 newkid_onthe_block

Reputation: 0
• Posts: 9
• Joined: 13-March 11

## Re: creating a basic triangle using asterisks

Posted 10 April 2011 - 03:16 PM

No no.. I apologize for not being clear. Let me try and explain better. I want to have a user input an integer. That integer will designate the size of the triangle. The triangle will be made of lines of asterisks starting at 1 asterisk. At each line the amount of asterisks will increase by 1 until the number of asterisks is equal to the integer inputted by the user. The next line will decrease the asterisks by 1 and so on until the number of asterisks is back to 1.

For example, if the user input is 5 then the output would look like this:

```*
**
***
****
*****
****
***
**
*

```

### #6 softwareEngineer()

Reputation: -28
• Posts: 247
• Joined: 08-April 11

## Re: creating a basic triangle using asterisks

Posted 10 April 2011 - 03:22 PM

Well, start by using a "for" statement until you get to the amount the user inputted.

Make sure you are using the variable++ notation.

You can see more about for statements here.

### #7 newkid_onthe_block

Reputation: 0
• Posts: 9
• Joined: 13-March 11

## Re: creating a basic triangle using asterisks

Posted 10 April 2011 - 03:41 PM

Well if the user input is the variable how can I implement that into the parameter for the for loop?

### #8 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: creating a basic triangle using asterisks

Posted 10 April 2011 - 04:13 PM

Your last question is confusing. You'll have to explain better what you don't understand.

You can print an increasing (and then decreasing number of asterisks by using a print statement without a return:

```for x, x increasing by 1
{
System.print( "*" );
}
// then print a linefeed
System.println();

```

This is a great opportunity to use a method, something like

public void printANumberOfAsterisks( int theNumberToPrint )

The method would print the number of asterisks specified on a single line followed by a linefeed.

### #9 newkid_onthe_block

Reputation: 0
• Posts: 9
• Joined: 13-March 11

## Re: creating a basic triangle using asterisks

Posted 10 April 2011 - 04:45 PM

I'm not sure. I have yet to learn using multiple methods.

Obviously, I need to assign "*" as a String variable. I'm just confused on how to print the desired amount of asterisks per line..

```/** This program takes a number from 1 to 50 from the user
* and output asterisks in the shape of a triangle with the
* user inputed number being the largest amount of asterisks*/

import java.util.Scanner;//Get Scanner from java utility

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

Scanner input = new Scanner(System.in);//Create Scanner object

int line; //declare the number of lines
int numA; // declare the size of the triangle
String ast = "*";

System.out.println("Enter the size of a triangle (1 to 50): ");
numA = input.nextInt(); //size of triangle

for(line = 1; line == numA; line++){
}

}
}

```

I am really confused. I'm not sure where to progress from here. I don't know how to increase the number of asterisks per line.

### #10 softwareEngineer()

Reputation: -28
• Posts: 247
• Joined: 08-April 11

## Re: creating a basic triangle using asterisks

Posted 10 April 2011 - 05:00 PM

You could always Google The Java Tutorials.

Those are very helpful, so check it out.

### #11 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12278
• Posts: 45,364
• Joined: 27-December 08

## Re: creating a basic triangle using asterisks

Posted 10 April 2011 - 10:36 PM

The condition in your for loop is when the loop should continue. When that condition becomes false, the loop will temrinate. I'm guessing you want your counter in the for loop to be <= numA, not == to it.

You will need nested loops here. The outer loop should loop from 1 through numA. The inner loop should loop from 0 through line, printing out a single "*" on each iteration. Then after the inner loop, print a new line.

You will then need a second set of loops to go backwards.

Some logic for the top-half of the triangle:
```for i = 1 through numA, i++ on each iteration
for j = 0 through i, j++ on each iteration
print "*"
print a new line
end for

```

@softwareEngineer(): Don't simply send our members back to Google. A lot of them come from Google looking for help. If a post simply warrants a "Google it" reply (as is not the case here), just report it. If there is a link you have in mind, dig it up and post it. That adds much more value to the thread.

### #12 newkid_onthe_block

Reputation: 0
• Posts: 9
• Joined: 13-March 11

## Re: creating a basic triangle using asterisks

Posted 11 April 2011 - 09:32 AM

Thanks @macosxnerd101! I was able to complete the practice problem.. Thanks everyone for your help and input!

### #13 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12278
• Posts: 45,364
• Joined: 27-December 08

## Re: creating a basic triangle using asterisks

Posted 11 April 2011 - 10:01 AM