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: Integer pyramid 1-15

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Integer pyramid 1-15

    Hi all.

    Am almost there with the problem below.

    I need to print an integer pyramid between 1 and 15, but when i get above 10 (11 to 15) i have a spacing problem and not sure how to fix this.

    Any and all advice is welcome

    import java.util.Scanner;
    class Q5
    {
    public static void main (String [] Args)
    	{
    	Scanner input = new Scanner(System.in);
     
    	System.out.print("Enter the number of lines: ");
    	int numberOfLines = input.nextInt();
     
    	if ((numberOfLines < 1) || (numberOfLines > 15))
    		{
    		System.out.println("You must enter a number from 1 to 15");
    		System.exit(0);
    		}
     
    	for (int row = 1; row <= numberOfLines; row++)
    		{
    		for (int column = 1; column <= numberOfLines - row; column++) 
    			{
    			System.out.print("  ");
    			}
    		for (int num = row; num >= 1; num--)
    			{
    			System.out.print(" " + num);
    			}
    		for (int num = 2; num <= row; num++)
    			{
    			System.out.print(" " + num);
    			}
     
    		System.out.println("");
    		}
    	input.close();
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Integer pyramid 1-15

    i have a spacing problem
    Please post the output showing the problem and add some comments or examples showing what you want the output to look like.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,412
    My Mood
    Mellow
    Thanks
    143
    Thanked 603 Times in 590 Posts

    Default Re: Integer pyramid 1-15

    I tend to attack these kind of problems differently - not better, necessarily - by generalizing the steps and using simple math where possible:

    - calculate the leading spaces
    - print the spaces
    - print the characters

    Looking at your problems, I came up with this generalization:

    // each line begins with 2 spaces * total lines to be printed less
    // 2 spaces * the current row being printed

    For the above, if 15 lines are to be printed, 28 leading spaces will be printed for line 1, and 0 leading spaces will be printed for line 15. As you've already discovered, it's a bit more complicated than that, so the rest of my generalization is:

    // if the total number of lines to be printed is greater than 9,
    // an extra space must be added to the single-digit lines for each
    // of the double-digit lines

    // if the current row being printed is > 9 and < the total number of
    // lines, reduce leading spaces by 1 for each row > 9 except the last

    // print the leading spaces

    // print the required numbers

    // add a linefeed

Similar Threads

  1. [SOLVED] Number Pyramid Question
    By t-rank in forum Loops & Control Statements
    Replies: 2
    Last Post: December 2nd, 2013, 07:44 AM
  2. Loops in a Pyramid problem
    By BeyondInvisible in forum Loops & Control Statements
    Replies: 2
    Last Post: November 7th, 2012, 08:10 AM
  3. Need help on the pyramid number!
    By nicetan in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 14th, 2011, 03:43 PM
  4. Pyramid of Doubling Numbers
    By Override in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 29th, 2010, 10:01 PM
  5. help with a loop (pyramid of numbers)
    By ande6870 in forum Loops & Control Statements
    Replies: 2
    Last Post: October 7th, 2010, 08:17 PM