Click here to Skip to main content
15,920,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Devs

I want to print a pyramid like shown below

      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *

Now , consider the code

class StarPyramid
    public static void main(String arr[])
    int n=6;
    for(int i=0;i<n;i++)
        for(int j=n;j>=i;j--)
            System.out.print(" ");
        for(int k=0;k<i;k++)System.out.print("#");
        //If i use the loop mentioned below , then i get asymmetrical pyramid
        //for(int m=0;m<i;m++)System.out.print("#");        
        System.out.println(" ");

What I have tried:

I get asymmetrical pyramid with this. Please explain me the logic to print a symmetrical one and how is mine different in terms of logic. What i am not doing properly ?

Thanks in advance !!
Updated 25-Sep-16 11:24am

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^][^][^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

Advice: tale a sheet of paper and try to do it by hand, your program should use the same procedure.
Make a table with 3 columns "Line #", "#spaces" and "# stars" and fill it
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900