Using Nested Loop for Pyramid

pyramid nested loop

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 26851 Views - Last Post: 26 February 2010 - 11:52 AM Rate Topic: -----

#1 CherylP  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 30-September 08

Using Nested Loop for Pyramid

Post icon  Posted 17 November 2008 - 08:57 PM

I need help with my homework. I am trying to create this:
*
***
*****
*******

I have the very basics of this program, but do not know where to go from here.

int count;
		
for (count = 1; count <=4; count++)
{
	System.out.println("*");
}



I know that I have to make it double, and there are some stipulations, like my print statement can't contain more than one space or asterik, and there can't be more than one print statement that contain an asterik and/or space. Please point me in the right direction if possible. Any help is appreciated! (This is the last one before I get to pass out! :) )

Okay, that's supposed to look like a pyramid from the top to the bottom.

This post has been edited by CherylP: 17 November 2008 - 08:57 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Using Nested Loop for Pyramid

#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Using Nested Loop for Pyramid

Posted 17 November 2008 - 09:02 PM

If you take a quick look through the forum, you'll find many posts on this. There's even a tutorial by Martyr2 on this very subject that can be found on D.I.C.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Using Nested Loop for Pyramid

Posted 17 November 2008 - 09:17 PM

View PostCherylP, on 17 Nov, 2008 - 07:57 PM, said:

I need help with my homework. I am trying to create this:
*
***
*****
*******

I have the very basics of this program, but do not know where to go from here.

int count;
		
for (count = 1; count <=4; count++)
{
	System.out.println("*");
}



I know that I have to make it double, and there are some stipulations, like my print statement can't contain more than one space or asterik, and there can't be more than one print statement that contain an asterik and/or space. Please point me in the right direction if possible. Any help is appreciated! (This is the last one before I get to pass out! :) )

Okay, that's supposed to look like a pyramid from the top to the bottom.


You use the println() method that prints and then prints a new line
There is also a print() statement that does not add a new line
So you need two nested loop
- the first one for the line number
- the second that does the print("*") as many time as you need it
- the the println(""); to change line
- end of the first loop

got it ?
Was This Post Helpful? 0
  • +
  • -

#4 CherylP  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 30-September 08

Re: Using Nested Loop for Pyramid

Posted 17 November 2008 - 09:28 PM

My brain isn't computing tonight...I always look through the tutorials and try Googling, reading my text, etc. before posting. I understand a little better when I post here, though, thanks to all of your wonderful help! :) I will try again tomorrow.
Was This Post Helpful? 0
  • +
  • -

#5 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 725
  • View blog
  • Posts: 4,471
  • Joined: 24-October 08

Re: Using Nested Loop for Pyramid

Posted 18 November 2008 - 12:11 AM

it will be better actually to start the outer loop from 0, it will make it easier for the inner loop to display odd number of asterisk
for(int i = 0; i < 5; i++)
{
	 //the rest of the stuff


good luck with this, its an easy one yet if u practice a lot of these looping exercises you'll be able to solve any looping problem easily:)
Was This Post Helpful? 0
  • +
  • -

#6 karen kaye  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-July 09

Re: Using Nested Loop for Pyramid

Posted 12 July 2009 - 07:26 AM

View PostGloin, on 17 Nov, 2008 - 08:02 PM, said:

If you take a quick look through the forum, you'll find many posts on this. There's even a tutorial by Martyr2 on this very subject that can be found on D.I.C.

Was This Post Helpful? 0
  • +
  • -

#7 karen kaye  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-July 09

Re: Using Nested Loop for Pyramid

Posted 12 July 2009 - 07:32 AM

View PostCherylP, on 17 Nov, 2008 - 07:57 PM, said:

I need help with my homework. I am trying to create this:
*
***
*****
*******

I have the very basics of this program, but do not know where to go from here.

int count;
		
for (count = 1; count <=4; count++)
{
	System.out.println("*");
}



I know that I have to make it double, and there are some stipulations, like my print statement can't contain more than one space or asterik, and there can't be more than one print statement that contain an asterik and/or space. Please point me in the right direction if possible. Any help is appreciated! (This is the last one before I get to pass out! :) )

Okay, that's supposed to look like a pyramid from the top to the bottom.





try this code..

int x;


for(x=1;x<1;x++)
system.out.print("*");
system.out.println();

for(x=1;x<2;x++)
system.out.print("*");
system.out.println();

for(x=1;x<3;x++)
system.out.print("*");
system.out.println();

for(x=1;x<4;x++)
system.out.print("*");
system.out.println();

for(x=1;x<5;x++)
system.out.print("*");
system.out.println();

for(x=1;x<6;x++)
system.out.print("*");
system.out.println();

for(x=1;x<7;x++)
system.out.print("*");
system.out.println();

for(x=1;x<8;x++)
system.out.print("*");
system.out.println();

for(x=1;<9;x++)
system.out.print("*);
sysrem.out.println();

for(x=1;<10;x++)
system.out.print("*);
sysrem.out.println();


{
{
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,586
  • Joined: 27-December 08

Re: Using Nested Loop for Pyramid

Posted 12 July 2009 - 08:00 AM

@Karen: I don't like that approach. Computer programming is designed so we can automate processes. This automates very little. Try using nested for loops.
Was This Post Helpful? 0
  • +
  • -

#9 blues90  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 20
  • Joined: 25-June 09

Re: Using Nested Loop for Pyramid

Posted 12 July 2009 - 10:07 AM

Hye, maybe it just like this..

 public class Star{
	public static void main(String args[])
	{
		int col;

		for (col=1;col<=1;col++)
			{
				System.out.print("*");
			}
		System.out.println("");
		for (col=1;col<=3;col++)
			{
				System.out.print("*");
			}
			System.out.println("");
		for (col=1;col<=5;col++)
			{
				System.out.print("*");
			}
		System.out.println("");
		for (col=1;col<=7;col++)
			{
				System.out.print("*");
			}
		System.out.println("");

	}
} 


Any mistaken, i'm so sorry..ok..
Was This Post Helpful? 0
  • +
  • -

#10 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Using Nested Loop for Pyramid

Posted 12 July 2009 - 10:20 AM

Nice necro, people. :^: :lol:

But as macosxnerd101 said...let's try to use nested for loops. The code can be tightened to 4 or 5 lines if we use nested loops.

This post has been edited by Locke: 12 July 2009 - 10:23 AM

Was This Post Helpful? 0
  • +
  • -

#11 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Using Nested Loop for Pyramid

Posted 12 July 2009 - 10:27 AM

View Postkaren kaye, on 12 Jul, 2009 - 06:32 AM, said:

View PostCherylP, on 17 Nov, 2008 - 07:57 PM, said:

I need help with my homework. I am trying to create this:
*
***
*****
*******

I have the very basics of this program, but do not know where to go from here.

int count;
		
for (count = 1; count <=4; count++)
{
	System.out.println("*");
}



I know that I have to make it double, and there are some stipulations, like my print statement can't contain more than one space or asterik, and there can't be more than one print statement that contain an asterik and/or space. Please point me in the right direction if possible. Any help is appreciated! (This is the last one before I get to pass out! :) )

Okay, that's supposed to look like a pyramid from the top to the bottom.





try this code..

int x;


for(x=1;x<1;x++)
system.out.print("*");
system.out.println();

for(x=1;x<2;x++)
system.out.print("*");
system.out.println();

for(x=1;x<3;x++)
system.out.print("*");
system.out.println();

for(x=1;x<4;x++)
system.out.print("*");
system.out.println();

for(x=1;x<5;x++)
system.out.print("*");
system.out.println();

for(x=1;x<6;x++)
system.out.print("*");
system.out.println();

for(x=1;x<7;x++)
system.out.print("*");
system.out.println();

for(x=1;x<8;x++)
system.out.print("*");
system.out.println();

for(x=1;<9;x++)
system.out.print("*);
sysrem.out.println();

for(x=1;<10;x++)
system.out.print("*);
sysrem.out.println();


{
{


The goal is to be more productive, it takes less lines to just do it manually.
System.out.println("*\n**\n***\n****\n*****\n******\n*******\n********\n*********\n**********\n");


I'm really surprised this thread is still going.

Think about what is changing for every loop. You should also notice that it changes at a steady interval. Now think of how you could make that automated.

This post has been edited by syfran: 12 July 2009 - 10:29 AM

Was This Post Helpful? 0
  • +
  • -

#12 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Using Nested Loop for Pyramid

Posted 12 July 2009 - 10:31 AM

View Postsyfran, on 12 Jul, 2009 - 11:27 AM, said:

I'm really surprised this thread is still going.


It shouldn't be. If you look at the post dates...all will be revealed. ;)
Was This Post Helpful? 0
  • +
  • -

#13 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Using Nested Loop for Pyramid

Posted 12 July 2009 - 11:27 AM

@blues90, you way is not efficient at all, this question has been asked numerous times here around the forums and there are code snippets everywhere that shows you how to do it to end this thread that has taken much more posts than it deserves here is how it is basically done, enjoy! :P
	for(int i =0; i < 5; i++){
			for(int j = 0; j < i;j++)
				System.out.print("*");	
			System.out.println(); 	 
		}


The above nested for loops print a 4 star pyramid.

Good luck CherylP :)

This post has been edited by ayman_mastermind: 12 July 2009 - 11:41 AM

Was This Post Helpful? 0
  • +
  • -

#14 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Using Nested Loop for Pyramid

Posted 12 July 2009 - 11:28 AM

@syfran

That will work if you know that you have to make the same pyramid every single time, your using magic numbers in a sense there. Don't leave it open for variety, what if I want a 10 star pyramid?

Thus nested for loops. Efficient and not a single purpose use, can be used with any size of pyramid.

Lets see how many other people will say the same thing about this one thread. ;)
Was This Post Helpful? 0
  • +
  • -

#15 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Using Nested Loop for Pyramid

Posted 12 July 2009 - 11:38 AM

View PostFuzzyness, on 12 Jul, 2009 - 10:28 AM, said:

@syfran

That will work if you know that you have to make the same pyramid every single time, your using magic numbers in a sense there. Don't leave it open for variety, what if I want a 10 star pyramid?

Thus nested for loops. Efficient and not a single purpose use, can be used with any size of pyramid.

Lets see how many other people will say the same thing about this one thread. ;)


Ya, just showing how ridiculous the code that was posted was. I'm still resisting the urge just to put the answer to such an easy problem and let the thread die again.

Edit: Looks like someone already did.

This post has been edited by syfran: 12 July 2009 - 11:39 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2