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: Number Pyramid Question

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

    Default Number Pyramid Question

    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...


    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

    i have got the right side of it, but i'm stuck on how to get the left side.

    import java.util.Scanner;
    public class pyramid {
    	public static void main(String[] args) {
    	Scanner input = new Scanner(System.in);
     
    	System.out.print("enter lines: ");
    	int k = input.nextInt();
     
    	for (int i=1; i<=k; i++){
    		for (int j=1; j<=i; j++){
    			System.out.print(" " +j);}
    			System.out.print("\n");
    	}
    	}
    }


  2. #2
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Number Pyramid Question

    Quote Originally Posted by t-rank View Post
    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...


    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

    i have got the right side of it, but i'm stuck on how to get the left side.

    import java.util.Scanner;
    public class pyramid {
    	public static void main(String[] args) {
    	Scanner input = new Scanner(System.in);
     
    	System.out.print("enter lines: ");
    	int k = input.nextInt();
     
    	for (int i=1; i<=k; i++){
    		for (int j=1; j<=i; j++){
    			System.out.print(" " +j);}
    			System.out.print("\n");
    	}
    	}
    }
    ....
    I omitted the scanner part.
    Last edited by copeg; December 2nd, 2013 at 08:47 AM. Reason: Removed spoonfed code

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,495
    My Mood
    Mellow
    Thanks
    152
    Thanked 610 Times in 597 Posts

    Default Re: Number Pyramid Question

    @racoonic: Please don't post solutions (2x).

Similar Threads

  1. help with a loop (pyramid of numbers)
    By ande6870 in forum Loops & Control Statements
    Replies: 2
    Last Post: October 7th, 2010, 08:17 PM
  2. Question: Converting number to words.
    By shamed in forum Java Theory & Questions
    Replies: 6
    Last Post: January 1st, 2010, 03:28 AM
  3. Help With Odd/Even number program
    By JonoScho in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 23rd, 2009, 09:53 AM
  4. Reverse Number
    By java1 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 28th, 2009, 10:19 AM
  5. [SOLVED] How to string a decimal number in Java?
    By Lizard in forum Loops & Control Statements
    Replies: 6
    Last Post: May 14th, 2009, 03:59 PM