Pyramid of pin using Recursion












1















enter image description hereI want to print a pyramid of pins using recursion(no Loops). I got the code almost done, but my pyramid is upside down, and it is not formatted. How can I fix it.
Below is my source code:



public void pinPattern(int count) {
if(count == 1)
System.out.println("*");
else {
System.out.print("*");
pinPattern(count - 1);
}
}

public int numberOfPins(int n) {
if(n == 0)
return 0;
else {
pinPattern(n);
return n + numberOfPins(n - 1);
}
}

public static void main(String args) {
Recursion x = new Recursion();
System.out.println("The number of pin: " + x.numberOfPins(5));
}


Thanks in advance.










share|improve this question

























  • I added my current output as a png file. Hope that helps.

    – Arif Shahriar
    Nov 16 '18 at 0:30
















1















enter image description hereI want to print a pyramid of pins using recursion(no Loops). I got the code almost done, but my pyramid is upside down, and it is not formatted. How can I fix it.
Below is my source code:



public void pinPattern(int count) {
if(count == 1)
System.out.println("*");
else {
System.out.print("*");
pinPattern(count - 1);
}
}

public int numberOfPins(int n) {
if(n == 0)
return 0;
else {
pinPattern(n);
return n + numberOfPins(n - 1);
}
}

public static void main(String args) {
Recursion x = new Recursion();
System.out.println("The number of pin: " + x.numberOfPins(5));
}


Thanks in advance.










share|improve this question

























  • I added my current output as a png file. Hope that helps.

    – Arif Shahriar
    Nov 16 '18 at 0:30














1












1








1








enter image description hereI want to print a pyramid of pins using recursion(no Loops). I got the code almost done, but my pyramid is upside down, and it is not formatted. How can I fix it.
Below is my source code:



public void pinPattern(int count) {
if(count == 1)
System.out.println("*");
else {
System.out.print("*");
pinPattern(count - 1);
}
}

public int numberOfPins(int n) {
if(n == 0)
return 0;
else {
pinPattern(n);
return n + numberOfPins(n - 1);
}
}

public static void main(String args) {
Recursion x = new Recursion();
System.out.println("The number of pin: " + x.numberOfPins(5));
}


Thanks in advance.










share|improve this question
















enter image description hereI want to print a pyramid of pins using recursion(no Loops). I got the code almost done, but my pyramid is upside down, and it is not formatted. How can I fix it.
Below is my source code:



public void pinPattern(int count) {
if(count == 1)
System.out.println("*");
else {
System.out.print("*");
pinPattern(count - 1);
}
}

public int numberOfPins(int n) {
if(n == 0)
return 0;
else {
pinPattern(n);
return n + numberOfPins(n - 1);
}
}

public static void main(String args) {
Recursion x = new Recursion();
System.out.println("The number of pin: " + x.numberOfPins(5));
}


Thanks in advance.







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 0:32









KevinO

3,27642031




3,27642031










asked Nov 16 '18 at 0:11









Arif ShahriarArif Shahriar

63




63













  • I added my current output as a png file. Hope that helps.

    – Arif Shahriar
    Nov 16 '18 at 0:30



















  • I added my current output as a png file. Hope that helps.

    – Arif Shahriar
    Nov 16 '18 at 0:30

















I added my current output as a png file. Hope that helps.

– Arif Shahriar
Nov 16 '18 at 0:30





I added my current output as a png file. Hope that helps.

– Arif Shahriar
Nov 16 '18 at 0:30












1 Answer
1






active

oldest

votes


















0














So there are two considerations here. First, the need to have the pyramid go the other direction. If it is printing upside-down, then instead of counting down (which goes from max to min), invert the approach. Something like:



public static int pinPatternUp(int numPins, int maxPins)
{
if (numPins <= maxPins) {
// this will add spaces
spaces(maxPins - numPins);
pins(numPins);
return numPins + (pinPattern(numPins + 1, maxPins));
}
return 0;
}


The logic for the pins method (which in the OP's code was 'pinPattern) in the OP's approach is essentially fine for either direction. However, making this change (so that there is a space after each*`) will allow for a bit better formatting.



System.out.print("* ");


The second question is one of formatting. In this case, we can look at the pyramid as requiring N spaces before the first output. And with a bit of investigation, it appears that a given line will have the total number of rows - the current row in terms of spaces. Using recursion, it is possible to do something like:



public static void spaces(int num)
{
if (num == 0) {
return;
}
System.out.print(" ");
spaces(num - 1);
}


Note that one can now have an "up" or "down" facing pyramid depending upon whether the pinPattern is increasing or decreasing.



Note: due to some editing, the method names do not exactly align with the OP's.



public static void main(String args)
{
pinPatternUp(1, 5);
pinPatternDown(5, 5);
}


Output if run up and then down:



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





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329627%2fpyramid-of-pin-using-recursion%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    So there are two considerations here. First, the need to have the pyramid go the other direction. If it is printing upside-down, then instead of counting down (which goes from max to min), invert the approach. Something like:



    public static int pinPatternUp(int numPins, int maxPins)
    {
    if (numPins <= maxPins) {
    // this will add spaces
    spaces(maxPins - numPins);
    pins(numPins);
    return numPins + (pinPattern(numPins + 1, maxPins));
    }
    return 0;
    }


    The logic for the pins method (which in the OP's code was 'pinPattern) in the OP's approach is essentially fine for either direction. However, making this change (so that there is a space after each*`) will allow for a bit better formatting.



    System.out.print("* ");


    The second question is one of formatting. In this case, we can look at the pyramid as requiring N spaces before the first output. And with a bit of investigation, it appears that a given line will have the total number of rows - the current row in terms of spaces. Using recursion, it is possible to do something like:



    public static void spaces(int num)
    {
    if (num == 0) {
    return;
    }
    System.out.print(" ");
    spaces(num - 1);
    }


    Note that one can now have an "up" or "down" facing pyramid depending upon whether the pinPattern is increasing or decreasing.



    Note: due to some editing, the method names do not exactly align with the OP's.



    public static void main(String args)
    {
    pinPatternUp(1, 5);
    pinPatternDown(5, 5);
    }


    Output if run up and then down:



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





    share|improve this answer






























      0














      So there are two considerations here. First, the need to have the pyramid go the other direction. If it is printing upside-down, then instead of counting down (which goes from max to min), invert the approach. Something like:



      public static int pinPatternUp(int numPins, int maxPins)
      {
      if (numPins <= maxPins) {
      // this will add spaces
      spaces(maxPins - numPins);
      pins(numPins);
      return numPins + (pinPattern(numPins + 1, maxPins));
      }
      return 0;
      }


      The logic for the pins method (which in the OP's code was 'pinPattern) in the OP's approach is essentially fine for either direction. However, making this change (so that there is a space after each*`) will allow for a bit better formatting.



      System.out.print("* ");


      The second question is one of formatting. In this case, we can look at the pyramid as requiring N spaces before the first output. And with a bit of investigation, it appears that a given line will have the total number of rows - the current row in terms of spaces. Using recursion, it is possible to do something like:



      public static void spaces(int num)
      {
      if (num == 0) {
      return;
      }
      System.out.print(" ");
      spaces(num - 1);
      }


      Note that one can now have an "up" or "down" facing pyramid depending upon whether the pinPattern is increasing or decreasing.



      Note: due to some editing, the method names do not exactly align with the OP's.



      public static void main(String args)
      {
      pinPatternUp(1, 5);
      pinPatternDown(5, 5);
      }


      Output if run up and then down:



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





      share|improve this answer




























        0












        0








        0







        So there are two considerations here. First, the need to have the pyramid go the other direction. If it is printing upside-down, then instead of counting down (which goes from max to min), invert the approach. Something like:



        public static int pinPatternUp(int numPins, int maxPins)
        {
        if (numPins <= maxPins) {
        // this will add spaces
        spaces(maxPins - numPins);
        pins(numPins);
        return numPins + (pinPattern(numPins + 1, maxPins));
        }
        return 0;
        }


        The logic for the pins method (which in the OP's code was 'pinPattern) in the OP's approach is essentially fine for either direction. However, making this change (so that there is a space after each*`) will allow for a bit better formatting.



        System.out.print("* ");


        The second question is one of formatting. In this case, we can look at the pyramid as requiring N spaces before the first output. And with a bit of investigation, it appears that a given line will have the total number of rows - the current row in terms of spaces. Using recursion, it is possible to do something like:



        public static void spaces(int num)
        {
        if (num == 0) {
        return;
        }
        System.out.print(" ");
        spaces(num - 1);
        }


        Note that one can now have an "up" or "down" facing pyramid depending upon whether the pinPattern is increasing or decreasing.



        Note: due to some editing, the method names do not exactly align with the OP's.



        public static void main(String args)
        {
        pinPatternUp(1, 5);
        pinPatternDown(5, 5);
        }


        Output if run up and then down:



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





        share|improve this answer















        So there are two considerations here. First, the need to have the pyramid go the other direction. If it is printing upside-down, then instead of counting down (which goes from max to min), invert the approach. Something like:



        public static int pinPatternUp(int numPins, int maxPins)
        {
        if (numPins <= maxPins) {
        // this will add spaces
        spaces(maxPins - numPins);
        pins(numPins);
        return numPins + (pinPattern(numPins + 1, maxPins));
        }
        return 0;
        }


        The logic for the pins method (which in the OP's code was 'pinPattern) in the OP's approach is essentially fine for either direction. However, making this change (so that there is a space after each*`) will allow for a bit better formatting.



        System.out.print("* ");


        The second question is one of formatting. In this case, we can look at the pyramid as requiring N spaces before the first output. And with a bit of investigation, it appears that a given line will have the total number of rows - the current row in terms of spaces. Using recursion, it is possible to do something like:



        public static void spaces(int num)
        {
        if (num == 0) {
        return;
        }
        System.out.print(" ");
        spaces(num - 1);
        }


        Note that one can now have an "up" or "down" facing pyramid depending upon whether the pinPattern is increasing or decreasing.



        Note: due to some editing, the method names do not exactly align with the OP's.



        public static void main(String args)
        {
        pinPatternUp(1, 5);
        pinPatternDown(5, 5);
        }


        Output if run up and then down:



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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 16 '18 at 1:02

























        answered Nov 16 '18 at 0:57









        KevinOKevinO

        3,27642031




        3,27642031
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329627%2fpyramid-of-pin-using-recursion%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            The Sandy Post

            Danny Elfman

            Pages that link to "Head v. Amoskeag Manufacturing Co."