10 Replies - 66981 Views - Last Post: 05 March 2009 - 01:08 PM Rate Topic: ***-- 2 Votes

#1 webeinater  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 11-February 09

Triangle of stars in Java

Posted 11 February 2009 - 03:33 PM

I am new to programming in java. I have an assignment where I have to make a tree of stars using only 2-3 print statements and using loops(not just print statements). Here is the code I have so far, I am using "Ready to Program" as the compiler.

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

int c, d, e;
for (c = 1 ; c <= 8 ; c++)
{
for (d = 1 ; d <= c ; d++)
{
System.out.print ("*");
}
System.out.println("");
}
for (e = 1 ; e <= 4 ; e++)
{
System.out.println ("***");
}

}
}

The problem though is that it only prints half of the triangle like this
*
**
***
****
*****
******
*******
********
***
***
***
***
Can anyone help me with getting the whole tree to look like a pyramid or isoceles triangle(full triangle not just half) or how to get the left half of the triangle but still use only 2-3 print statements and loops. If you can try to keep the coding simple as I have only learned for, while and if loops:


Thanks for the help.

Is This A Good Question/Topic? 0
  • +

Replies To: Triangle of stars in Java

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Triangle of stars in Java

Posted 11 February 2009 - 03:43 PM

you can do the same thing but in reverse, in other words try to do another loop that prints
*****
****
***
**
*
Was This Post Helpful? 0
  • +
  • -

#3 webeinater  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 11-February 09

Re: Triangle of stars in Java

Posted 11 February 2009 - 04:00 PM

View Postmostyfriedman, on 11 Feb, 2009 - 02:43 PM, said:

you can do the same thing but in reverse, in other words try to do another loop that prints
*****
****
***
**
*



Sorry, I was wondering how to print the left side of the triangle so that the tree looked like this when it was done.(just ignore the ....'s)

.................................*
...............................***
.............................*****
...........................*******
.........................*********
.......................***********
.....................*************
...................***************
...............................***
...............................***
...............................***
...............................***
Thanks
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Triangle of stars in Java

Posted 11 February 2009 - 04:13 PM

ohh, ok..k i'll give you a hint, lets say i wanna print 5 lines of asterisks..as you see the number of asterisks in each row are odd, and in your inner loop if you wanna print odd number of asterisk then you should change the condition to
for(int d = 1; d <= 2*c+1; d++)


coz if you multiply any number by 2 and then add 1, you get and odd number..note that your outer loop counter should start with 0 otherwise your asterisk would start by printing 3 asterisks..i hope this was helpful
Was This Post Helpful? 0
  • +
  • -

#5 webeinater  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 11-February 09

Re: Triangle of stars in Java

Posted 11 February 2009 - 04:23 PM

View Postmostyfriedman, on 11 Feb, 2009 - 03:13 PM, said:

ohh, ok..k i'll give you a hint, lets say i wanna print 5 lines of asterisks..as you see the number of asterisks in each row are odd, and in your inner loop if you wanna print odd number of asterisk then you should change the condition to
for(int d = 1; d <= 2*c+1; d++)


coz if you multiply any number by 2 and then add 1, you get and odd number..note that your outer loop counter should start with 0 otherwise your asterisk would start by printing 3 asterisks..i hope this was helpful


Thanks, that helped more but it still doesn't get the full triangle. It has the right number of asterisks in each row now but it is still a right triangle against the left. Is there any simple way I can space out the stars to line them up properly without adding any print statements. Also is there any way I can still get the right triangle but reduce the print statements I already have.
Thanks again for your help
Was This Post Helpful? 0
  • +
  • -

#6 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Triangle of stars in Java

Posted 11 February 2009 - 04:33 PM

well i have solved this a long time ago, and the solution isnt very clever since i was still a beginner in that stuff
 for( int i = 0; i <= n/2; i++ )
		{
		  for( int j = ( n/2 )-i; j >= 1; j-- )
			System.out.print( " " );

		  for( int k = 1; k <= 2*( i+1 ) - 1; k++ )
			System.out.print( '*' );

		System.out.println();
		}


gimme a few minutes and i'll try to come up with a better solution for it

This post has been edited by mostyfriedman: 11 February 2009 - 04:34 PM

Was This Post Helpful? 0
  • +
  • -

#7 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Triangle of stars in Java

Posted 11 February 2009 - 05:48 PM

sorry for the late reply, here's a better solution
public class triangle
{
	public static void main(String[]args)
	{
		 int n = 5; 
		 int spaces = n-1;
		 int ast;				  
		 for(int i = 0; i < n; i++)
		 {	
			ast = 2*i+1;
			for(int j = 1; j <= spaces+ast; j++)
			{
				if(j <= spaces)
					System.out.print(' ');
				else
					System.out.print('*');
			}
			System.out.println();
			spaces--;
		}	 
	}
}


This post has been edited by mostyfriedman: 11 February 2009 - 06:43 PM

Was This Post Helpful? 0
  • +
  • -

#8 webeinater  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 11-February 09

Re: Triangle of stars in Java

Posted 11 February 2009 - 06:03 PM

View Postmostyfriedman, on 11 Feb, 2009 - 04:48 PM, said:

sorry for the late reply, here's a better solution
public class triangle
{
	public static void main(String[]args)
	{
		 int n = 5; 
		 int spaces = n-1;
		 int ast;				  
		 for(int i = 0; i < n; i++)
			 {	
			ast = 2*i+1;
			for(int j = 1; j <= spaces+ast; j++)
			{
				if(j <= spaces)
					System.out.print(' ');
				else
					System.out.print('*');
			}
			System.out.println();
			spaces--;
			 }
	 
	}
}



Thanks for the great help
Was This Post Helpful? 0
  • +
  • -

#9 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Triangle of stars in Java

Posted 11 February 2009 - 06:04 PM

no problem ;), i just hope that you understand what's going on, if you have any questions, dont hesitate to ask
Was This Post Helpful? 0
  • +
  • -

#10 6moose  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 05-March 09

Re: Triangle of stars in Java

Posted 05 March 2009 - 12:49 PM

View Postmostyfriedman, on 11 Feb, 2009 - 05:04 PM, said:

no problem ;), i just hope that you understand what's going on, if you have any questions, dont hesitate to ask



I know this is an older post, but also have a homework assignment like this is except of needing a tree i just need it to draw like this
user input = 6
*
**
***
****
*****
******
*****
****
***
**
*
could someone help me understand this?
Was This Post Helpful? 1
  • +
  • -

#11 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Triangle of stars in Java

Posted 05 March 2009 - 01:08 PM

you cant hijack a thread here :P, create a new topic and also
[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1