The code has lost all its formatting. Logically nested statements should be indented. See the code sample I posted.
The statements should not all start in the first column.
The statements controlled by the for statement are not wrapped in {}s.
There should not be a statement on the same line following a {
Proper formatting makes code much easier to read and understand.
The way to see what the code is doing is to use paper and pencil to write down the values of the variables at the end of the line for each time a line is executed.
Write the values in columns with each column showing the values on one iteration of the loop.
for (M=1; M<=N-1;M++) // N = 6, M = 1 || N = 5, M = 1 etc
Continue for all the source lines.