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

How can I output The array like this? Java

The output should look like this

91 92 93 94 95 96 97 98 99 100
81 82 83 84 85 86 87 88 89 90
71 72 73 74 75 76 77 78 79 80
61 62 63 64 65 66 67 68 69 70
51 52 53 54 55 56 57 58 59 60
41 42 43 44 45 46 47 48 49 50
31 32 33 34 35 36 37 38 39 40
21 22 23 24 25 26 27 28 29 30
11 12 13 14 15 16 17 18 19 20
1  2  3  4  5  6  7  8  9  10

I have to use two demensional arrays to print them. This is my code:

public class Main
{
    public static void main(String[] args) {
    int[][] arr = new int [10][10];
    for(int i = 0; i < arr.length; i++){
            for(int j = 0; j < arr.length; j++){
                arr[i][j] =  (i / 10) + (j + 91);
                System.out.print(arr[i][j] + " ");
                
            }
            System.out.println();
        }
    }
}

But the output look like this:

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

91 92 93 94 95 96 97 98 99 100 
91 92 93 94 95 96 97 98 99 100 
91 92 93 94 95 96 97 98 99 100 
91 92 93 94 95 96 97 98 99 100 
91 92 93 94 95 96 97 98 99 100 
91 92 93 94 95 96 97 98 99 100 
91 92 93 94 95 96 97 98 99 100 
91 92 93 94 95 96 97 98 99 100 
91 92 93 94 95 96 97 98 99 100 
91 92 93 94 95 96 97 98 99 100 

What am I doing Wrong?

>Solution :

   public class Main
     {
       public static void main(String[] args) {
       int[][] arr = new int [10][10];
       int initialValue = 91;
       for(int i = 0; i < arr.length; i++){

          for(int j = 0; j < arr.length; j++){
             arr[i][j] =  (i / 10) + (j + initialValue);
             System.out.print(arr[i][j] + " ");

          }
         initialValue = initialValue - 10;
         System.out.println();
      } 
   }
}

output:

91 92 93 94 95 96 97 98 99 100

81 82 83 84 85 86 87 88 89 90

71 72 73 74 75 76 77 78 79 80

61 62 63 64 65 66 67 68 69 70

51 52 53 54 55 56 57 58 59 60

41 42 43 44 45 46 47 48 49 50

31 32 33 34 35 36 37 38 39 40

21 22 23 24 25 26 27 28 29 30

11 12 13 14 15 16 17 18 19 20

1 2 3 4 5 6 7 8 9 10

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