Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: help with a loop (pyramid of numbers)

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help with a loop (pyramid of numbers)

    i have been working on this program for like 4 hours and i just cannot come up with a solution...i do know that loops have to be used....i just having way to much problems


    Problem:
    Write a program that prompts the user to enter an intger from 1 to 15 and displays a pyramid, as shown in the following sample run:

    Enter the number of lines: 7
    ______1
    _____212
    ____32123
    ___4321234
    __543212345
    _65432123456
    7654321234567

    those are suppose to be spaces instead of underscores...its only way i could make it look right on this post


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: help with a loop (pyramid of numbers)

    What have you come up with as a design so far? The program needs to use nested loops. Outer for the rows and inner for each row.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: help with a loop (pyramid of numbers)

    Ok, well provide us with the code you have first, and then we can help you more from there.

    Lets have a look at the design of a pyramid. First of all, since this is a 2 dimensional image (vertical and horizontal), we need a nested loop. A nested loop is a loop inside a loop. For example, the following code:
    for(int i=0;i<5;i++)
    {
    	for(int x=0;x<5;x++)
    	{
    		System.out.println("Loop");
    	}
    }
    will print out the word: Loop 25 times. The reason is because it will print it out 5 times for every loop of the outer loop. 5 x 5 is equal to 25, so it will run the inner loop a total of 25 times.

    Now, since we are making a pyramid we want the limit of our inner loop to be dynamic. Specifically, after row 1, each row has 2 more numbers than the preceding row. So, we want to probably base our inner loop condition off of our outer loop count variable.

    There is another thing to consider. How many spaces do we need before the first number on each line? Well, the design of a pyramid says the base is twice as wide as the height. But, we want our first number to be in the center of the pyramid, so that number will be half of the base, which is conveniently equal to height. That is for the first row. Continuing the design of the pyramid tells us the number of spaces before a number on any give line is 1 less than the number of spaces before the number of the preceding line.

    The last thing is the numbers. Notice how the first number on each row is equal to the number of the row in respect to the top, where: 1 is the top and the last row is the height.

    See if you can work with those instructions.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. adding up odd and even numbers
    By darlinho in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 30th, 2010, 03:28 PM
  2. Odd and Even Numbers Logics in FOR and IF Loop methods
    By mparthiban in forum Loops & Control Statements
    Replies: 2
    Last Post: May 12th, 2010, 05:19 AM
  3. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  4. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM
  5. Roman Numbers
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: December 12th, 2009, 02:19 AM