# Interchange the diagonal of a matrix in node js Js program for Interchange the diagonal of a matrix. Here mentioned other language solution.

``````/*
Node JS program for
Swap diagonal elements of matrix using recursion
*/
// Method which are perform swap operation
// Here x and y indicate start and end rows
// p and q indicates start and end columns
function swapDiagonal(matrix, x, y, p, q)
{
if (x >= y || p >= q)
{
return;
}
var temp = matrix[x][p];
// Swap the element value
matrix[x][p] = matrix[x][q];
matrix[x][q] = temp;
temp = matrix[y][p];
// Swap the element value
matrix[y][p] = matrix[y][q];
matrix[y][q] = temp;
swapDiagonal(matrix, x + 1, y - 1, p + 1, q - 1);
}
// Display matrix elements
function showMatrix(matrix, size)
{

console.log(matrix);
}

function main()
{
// Define matrix element
var matrix = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]
];
// Assume N X N matrix
var n = matrix.length;
// Display element
showMatrix(matrix, n);
// interchange request
swapDiagonal(matrix, 0, n - 1, 0, n - 1);
// Display element
showMatrix(matrix, n);
}
// Start program execution
main();``````

Output

``````[ [ 1, 2, 3, 4 ],
[ 5, 6, 7, 8 ],
[ 9, 10, 11, 12 ],
[ 13, 14, 15, 16 ] ]
[ [ 4, 2, 3, 1 ],
[ 5, 7, 6, 8 ],
[ 9, 11, 10, 12 ],
[ 16, 14, 15, 13 ] ]``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.