Matrix not rotating 90 degree to Right

Solution 1:

(turning the comment by Ricky Mo into an answer with minimal changes and explanation)

You are very close. No need to fundamentally change your approach.
However, the output you get shows clearly that your code does not write to the last column.
An obvious conclusion is that your loop setups are not correct.
And indeed (as Ricky Mo put succinctly):

change for(i=M-1;i>0;i--) to for(i=M-1;i>=0;i--)

Obviously it iterates once more often.
And that ends up in the right (i.e. the 0 values column) because of the indexing magic, which you correctly implemented.

And the output is:

ENTER THE NUMBER OF ROW AND COLUMN OF MATRIX
ENTER THE ELEMENTS IN MATRIX 

------------------------------------------------------
ORIGINAL MATRIX 

1 2 3 
4 5 6 
7 8 9 

------------------------------------------------------
ROTATED MATRIX 

7 4 1 
8 5 2 
9 6 3 

e.g. here:
https://www.tutorialspoint.com/compile_java_online.php

Solution 2:

To rotate, you need something like this:

int arr[][];
int dest[][];

arr = new int[M][M];
dest = new int[M][M];

for (int i=0; i<M, i++) {
  for (int j=0; j<M, j++) {
    dest[M-j-1][i] = arr[i][j];
  }
}

also, you count down loops need to include zero

for(i=M-1;i>0;i--)

should be

for(i=M-1;i>=0;i--)