mardi 21 janvier 2020

I have a problem in number pattern in java [closed]

Ques =

1                       1
1   2               2   1
1   2   3       3   2   1
1   2   3   4   3   2   1

public class PatternMountain {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scn = new Scanner(System.in);
        int N = scn.nextInt();
        int nst = 1 ; 
        int nsp = 2*N-3;
        int val = 1;
        int row = 1 ;
        if(N<10) {
        while(row  <= N ) {

            int cst = 1 ;
            while(cst<=nst) {
                System.out.print(val);
                cst++;


            }

            int csp = 1;
            while(csp<=nsp) {
                System.out.print(" ");
                csp++;
            }
            //stars work
             cst = 1;
             if( row == N) {
                 cst = 2 ;
             }
            while(cst<=nst) {
                System.out.print(val);
                        cst++;

            }

            System.out.println();

              val++;  
             nsp = nsp - 2 ;
             nst = nst + 1 ;

             row++;

        }
    }

}}

But the output which I am getting is -

1     1
22   22
333 333
4444444

Aucun commentaire:

Enregistrer un commentaire