Results 1 to 2 of 2

Thread: Pyramid in Java

  1. #1
    Haizaaa is offline Member
    Join Date
    Nov 2016
    Posts
    4
    Rep Power
    0

    Default Pyramid in Java

    Hi. Good day everyone. I can't seem to form a pyramid in my code. I hope anyone can help me with this problem. The question is about asking user to enter integer within 1-15 and display a pyramid. The output should be like this:

    Line: 5
    1
    212
    32123
    4321234
    543212345

    This is my code.

    import java.util.Scanner;

    public class Q3 {
    public static void main(String[]args){
    Scanner k = new Scanner(System.in);
    int line;
    do{
    System.out.print("Enter the number of lines: ");
    line = k.nextInt();
    }while(line>15 || line<1 );

    int i;
    int value = 1;
    int n[] = new int[line];

    for(i=0;i<line;i++){
    n[i] = value;
    value++;
    }
    int m, in, dec, space;

    for(m=0;m<line;m++){
    for(space=0;space<(line-m/2);space++){
    System.out.print("*");
    }
    if(m == 0)
    {
    System.out.println(n[0]);
    }
    else{
    for(dec = m; dec>=0; dec--)
    {
    System.out.print(n[dec] + " ");
    }

    for(in=dec+2; in<=m; in++)
    {
    System.out.print(n[in] + " ");
    }
    System.out.println("");
    }
    }

    }
    }


    My output became like this:
    Enter the number of lines: 5
    *****1
    *****2 1 2
    ****3 2 1 2 3
    ****4 3 2 1 2 3 4
    ***5 4 3 2 1 2 3 4 5

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    5,847
    Rep Power
    10

    Default Re: Pyramid in Java

    Please use code tags to preserve formatting. I have done it for you here.

    Java Code:
    public class Q3 {
       public static void main(String[] args) {
          Scanner k = new Scanner(System.in);
          int line;
          do {
             System.out.print("Enter the number of lines: ");
             line = k.nextInt();
          } while (line > 15 || line < 1);
    
          int i;
          int value = 1;
          int n[] = new int[line];
    
          for (i = 0; i < line; i++) {
             n[i] = value;
             value++;
          }
          int m, in, dec, space;
    
          for (m = 0; m < line; m++) {
             for (space = 0; space < (line - m / 2); space++) {
                System.out.print("*");
             }
             if (m == 0) {
                System.out.println(n[0]);
             }
             else {
                for (dec = m; dec >= 0; dec--) {
                   System.out.print(n[dec] + " ");
                }
    
                for (in = dec + 2; in <= m; in++) {
                   System.out.print(n[in] + " ");
                }
                System.out.println("");
             }
          }
    
       }
    }
    The problem lies in line 21. You are not calculating the number of spaces correctly. The conditional in the for loop is incorrect. Think about how many characters (spaces and digits) the longest line has. Then use that value with the number of lines to adjust the conditional.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 0
    Last Post: 09-13-2013, 10:50 AM
  2. Draw a pyramid
    By MisterNikos in forum New To Java
    Replies: 23
    Last Post: 04-02-2012, 01:44 PM
  3. Learning Java, wondering about pyramid assignment.
    By fisch3r in forum New To Java
    Replies: 6
    Last Post: 04-02-2012, 01:08 AM
  4. Need Help With Some Pyramid
    By lord raza in forum New To Java
    Replies: 1
    Last Post: 11-07-2011, 02:03 PM
  5. pyramid
    By Dashinator in forum New To Java
    Replies: 8
    Last Post: 11-15-2010, 10:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •