Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java for loop decrementing

can you guys pls help me

i want to code where the user will enter a number and then using for loop it will display the number in decrement for example:

Enter a number: 4

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

OUTPUT:
4
4-3
4-3-2
4-3-2-1
3
3-2
3-2-1
2
2-1
1-1

code it using for loop

this is my attempt code i’m getting confused:

  for (int i = num; i >= 1; i--) {
      for (int j = 1; j <= i; j++)
         {
               System.out.print(i+ "-" );
          for (int k = num; k>=i; k--)
             {
                System.out.print(k);
            }
            System.out.println();
        }

>Solution :

I can’t produce your last line(1-1) from the pattern with the below code, as if you think doesn’t match the pattern as well rather than that it can produce the exact same results:

    for (int x = n; x >= 1; x--) {
        for (int i = n; i >= 1; i--) {
            for (int j = n; j >= i; j--) {
                System.out.print (j);
                if (j > i) {
                    System.out.print("-");
                }
            }
            System.out.println();
        }
        n--;
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading