mercredi 28 juin 2017

Printing a * pattern

So I have to print the following pattern by accepting a value of n Input : 7 Output has to be this :

    *
    **
    ***
    ****
    *****
    ******
    *******
    ******
    *****
    ****
    ***
    **
    *
code:

public static void printPattern(int n)

{

for(int i=1;i<=n;i++)

    {
        for(int j=1;j<=i;j++)

           {
            System.out.println("*");

            }
            System.out.println("\n");

      } 

for (int a = n-1; a >= 1; a--)

    {
        for (int b = 1; b <= a; b++)
             {
              System.out.print("*");
             }
               System.out.println("\n");
    }
}

But for some reason it prints this pattern(say n=8):

*

* * * * *

* * * * * *

* * * * * * *

* * * * * * * *






**

* What is the mistake here? Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire