mercredi 27 mars 2019

Pyramid Pattern in Java with Star followed by a underscore

I want to print below pattern

*
*_*
*_*_*
*_*_*_*
*_*_*_*_*

but the issue here is I am unable to eliminate last _ symbol below is the output that I am getting

*
*_*_
*_*_*_
*_*_*_*_
*_*_*_*_*_

int row =5;
String star="*";
String undrscr="_";
String s;
for(int i=1;i<=row;i++)
{//System.out.print(undrscr);
    for(int j=1;j<=i;j++)
    {           
        if(i==1)
        {
        System.out.print(star);
        }
        if(i>1)
        {
            System.out.print(star+""+undrscr);
        if(j==i)
        {
            System.out.print("");
        }
        }
    }

    System.out.println();
}

Aucun commentaire:

Enregistrer un commentaire