Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Here’s my code:

class p1 {

    public static void main(String args[]) {

        System.out.println("The Pattern is");

        for (int i = 0; i < 5; i++) {

            for (int j = 0; j < 5; j++) {

                if (j <= i) {

                    System.out.print("  $");

                } else {

                    System.out.print("   ");

                }

            }

            System.out.println();

        }

    }

}

This is the output I get:

  $            

  $  $         

  $  $  $      

  $  $  $  $   

  $  $  $  $  $

My expected output is: 

         $            

       $  $         

     $  $  $      

   $  $  $  $   

 $  $  $  $  $ 

Can anyone tell me how to code for the above output?  

1 Answer

0 votes
by (19.7k points)

Refer to the code below: 

public static void main(String[] args) {

     for(int i=0;i<5;i++) {

         for(int j=0;j<5-i;j++) {

             System.out.print(" ");

         }

        for(int k=0;k<=i;k++) {

            System.out.print("$ ");

        }

        System.out.println();  

    }

}

 This will print the output like this:

    $ 

   $ $ 

  $ $ $ 

 $ $ $ $ 

$ $ $ $ $

Interested in Java? Check out this Java Certification by Intellipaat.  

Related questions

0 votes
1 answer
0 votes
1 answer
asked Mar 23, 2021 in Java by sheela_singh (9.5k points)
0 votes
1 answer
asked Feb 25, 2021 in Java by RohitSingh (2.6k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...