# data[].attributes.body
public static void Ucheck(int[][] arr)
{
int i;
int j;
int k;
int temp = 0;

System.out.println();

for (i = 0; i < (arr.length-1); i++)
{
for (j = 1; j <= i; j++)
{
if (arr[i+j][i] != 0)
{
System.out.println("This is not an upper triangular matrix.");
System.exit(0);
}
}
}

System.out.println("This is an upper triangular matrix.");

for (i = 0; i < arr.length; i++)
{
for(j = 0; j < arr[i].length; j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}

# data[].attributes.body
public static void Lcheck(int[][] arr)
{
int i;
int j;
int k;
int temp = 0;

System.out.println();

for (i = 1; i < arr.length; i++)
{
for (j = 1; j <= i; j++)
{
if (arr[i-j][i] != 0)
{
System.out.println("This is not a lower triangular matrix.");
System.exit(0);
}
}
}

System.out.println("This is a lower triangular matrix.")
}

# data[].attributes.body
public static void interchangeColumns(int[][] arr, int c1, nt c2)
{
int i = 0;
int j = 0;
int temp = 0;

for (i = 0; i < arr.length; i++)
{
temp = arr[i][c1];
arr[i][c1] = arr[i][c2];
arr[i][c2] = temp;
}
for (i = 0; i < arr.length; i++)
{
for(j = 0; j < arr[i].length; j++)
{
if (arr[i][j] < 10)
{
System.out.print(" ");
}
System.out.print(arr[i][j]+",");
}
System.out.println();
}
}

# data[].attributes.body
import java.util.Scanner;

public class InterchangeDiagonals2
{
public static void interchangeDiagonals(int[][] arr)
{
int i = 0;
int j = 0;
int temp = 0;

//interchange
for (i = 0; i < arr.length; i++)
{
temp = arr[i][(arr[i].length - 1)-i];
arr[i][(arr[i].length - 1)-i] = arr[i][i];
arr[i][i] = temp;

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

}
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);

int[][] arr =
{
/*{ 3,24, 2, 6},
{ 5,23,45,65},
{23,54, 8,23},
{ 2,32,18, 9},*/
{ 1, 0, 0, 0},
{ 0, 1, 0, 0},
{ 0, 0, 1, 0},
{ 0, 0, 0, 1},
};
//print arr
for (int i = 0; i < arr.length; i++)
{
for(int j = 0; j < arr[i].length; j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}

interchangeDiagonals(arr);
}
}

# data[].attributes.body
public static int[][] spiral(int g){
int[][] x = new int[g][g];
int l = 0;
int k = 0;
int j = g-1;
int i = g-1;
int n = 0;
while(i>=k || j>=l){
for(int f = k; f<=i; f++){
n++;
x[l][f] = n;
}

for(int b = l; b<=j;b++){
x[b][i] = n;
n++;
}

i--;
for(int c = i; c>=k; c--){
x[j][c] = n;
n++;
}

l++;
j--;
for(int z = j; z>=l; z--){
x[z][k] = n;
n++;
}
k++;
n--;
}
return x;
}

# data[].attributes.body
import java.util.*;
import java.lang.Math.*;

public class LowerTriangle {
public static void main(String [] args){
/* Input */
Scanner inp = new Scanner(System.in);
System.out.println("Enter the Size of the Matrix you want:");
int size = inp.nextInt();
int[][]array = new int[size][size];
System.out.println("Enter the numbers in the Matrix:");
for (int a =0; a<size; a++) {
for (int b=0; b <size; b++) {
array[a][b] = inp.nextInt();
}
}
/* Print */
for (int c=0 ; c<size;c++) {
for (int d=0;d<size;d++){
System.out.print(array[c][d]+" ");
}
System.out.println ("            ");
}
/* Check Diagonal */
int sum =0;
for (int i =0; i<size; i++) {
for (int j =i+1; size>j; j++) {
sum = array[i][j] + sum;
}
}
System.out.println (sum);
if (sum>0) {
System.out.println ("This is not a Lower Triangular Matrix");
}
if (sum==0) {
System.out.println ("This is a Lower Triangular Matrix");
}
}
}

# data[].attributes.body
import java.util.*;
import java.lang.Math.*;

public class UpperTriangular
{
public static void main(String [] args){
/* Input */
Scanner inp = new Scanner(System.in);
System.out.println("Enter the Size of the Matrix you want:");
int size = inp.nextInt();
int[][]array = new int[size][size];
System.out.println("Enter the numbers in the Matrix:");
for (int a =0; a<size; a++) {
for (int b=0; b <size; b++) {
array[a][b] = inp.nextInt();
}
}
/* Print */
for (int c=0 ; c<size;c++) {
for (int d=0;d<size;d++){
System.out.print(array[c][d]+" ");
}
System.out.println ("            ");
}
/* Check Diagonal */
int sum =0;
for (int i =0; i<size; i++) {
for (int j =0; j<i; j++) {
sum = array[i][j] + sum;

}
}

if (sum>0) {
System.out.println ("This is not a Upper Triangular Matrix");
}
if (sum==0) {
System.out.println ("This is a Upper Triangular Matrix");
}
}
}

# data[].attributes.body
import java.util.*;
import java.lang.Math.*;

public class SWAPDIAG{
public static void main(String [] args){
int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8 ,9}};
int temp = 0;
for (int i=0 ; i<3 ; i++) {
for (int j=0 ; j<3 ; j++) {
if (i==j) {
temp = arr[i][j];
arr[i][j] = arr[i][arr[i].length-1-i];
arr[i][arr[i].length-1-i] = temp;
}
}
}
for (int c=0 ; c<3;c++) {
for (int d=0;d<3;d++){
System.out.print(arr[c][d]+" ");
}
System.out.println ("            ");
}
}

}

# data[].attributes.body
Clockwise 90˚:
public static int[][] rotate(int[][] a){
int[][] x = new int[a[0].length][a.length];
for(int i = 0; i<a.length; i++){
for(int j = 0; j<a[0].length; j++){
x[j][a.length-i-1] = a[i][j];
}
}
return x;
}

Anticlockwise 90˚:
public static int[][] minusrotate(int[][] a){
int[][] x = new int[a[0].length][a.length];
for(int i = 0; i<a.length; i++){
for(int j = 0; j<a[0].length; j++){
x[j][i] = a[i][a[0].length-j-1];
}
}
return x;
}

# data[].attributes.body
public class rotatingmatrix {

public static void main(String args[]) {

int a[][] = { { 3, 5, 2 }, { 5, 6, 3 }, { 5, 7, 2 } };

int rotated[][] = new int[3][3];

int k = 2;

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

rotated[j][k] = a[i][j];

}

k--;

}

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

System.out.print(rotated[i][j] + " ");

}

System.out.println();

}

}

}

# data[].attributes.body
public static void switchDiag(int[][] c) {
int i = 0;
int j = c[0].length-1;
int k = 0;
while(k<c.length && i<c[0].length){
int temp = c[k][i];
c[k][i] = c[k][j];
c[k][j] = temp;
i++;
j--;
k++;
}
for(int x = 0; x<c.length; x++){
System.out.println();
for(int y = 0; y<c[0].length; y++){
System.out.print(c[x][y] + " ");
}
}
}

# data[].attributes.body
class swapdiagonals

{

public static void main(String args[]){

int a[][]={{3,5,2}, {5,6,3}, {5,7,2}};

int ai,aii,bi,bii,t;

if(a.length()==a[0].length()){

for(int i=0;i<a.length();i++){

for(int j=0;j<a.length();j++){

if(i==j){

ai=i;

bi=j;

}

if(i+j==a.length()-1){

aii=i;

bii=j;

}

}

t=a[ai][bi];

a[ai][bi]=a[aii][bii];

a[aii][bii]=t;

}

}

}

}

# data[].attributes.body
Interchange Diagonals
interChangeDiagonals(arr){
for i from 0 to arr[0].length() {
temp = arr[i][i]
arr[i][i] = arr[i][arr[0].length() - i -1]
arr[i][arr[0].length() - i -1] = temp
}
return arr
}

arr[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}
interChangeDiagonals(arr)
for int i from 0 to arr[0].length() {
for int j from 0 to arr[0].length() {
output(arr[i][j])
}
}

Sum
sumOfArray(int arr[][]) {
sum = 0
for int i from 0 to arr[0].length() {
for int j from 0 to arr[0].length() {
sum += arr[i][j]
}
output sum
}
}

arr[][] = {{1,2,3}, {4,5,6}, {7,8,9}}
sumOfArray(arr)

Swap Rows
swapRows(int arr[][], int row1, int row2) {
temp  = arr[row2]
arr[row2] = arr[row1]
arr[row1] = temp
return arr
}

arr[][] = {{1,2,3}, {4,5,6}, {7,8,9}}
row1 = input("Please enter a row you would like to swap")
row2 = input("Please enter the row you would like to swap with")
swapRows(arr, row1, row2)
for int i from 0 to arr[0].length() {
for int j from 0 to arr[0].length() {
output(arr[i][j])
}
}

Swap Columns
swapCols(int arr[][], int col1, int col2) {
for i from 0 to arr[0].length {
temp  = arr[1][col1]
arr[i][col1] = arr[i][col2]
arr[i][col2]  = temp
}
return arr
}

arr[][] = {{1,2,3}, {4,5,6}, {7,8,9}}
col1 = input("Please enter a column you would like to swap")
col2 = input("Please enter the column you would like to swap with")
swapCols(arr, col1, col2)
for int i from 0 to arr[0].length() {
for int j from 0 to arr[0].length() {
output(arr[i][j])
}
}

Upper Triangle
upperTriangle(int arr[][]) {
for i from 0 to arr[0].length {
for j from from 0 to arr[0].length {
if j > i {
output(arr[i][j])
}
output()
}
}
}

arr[][] = {{1,2,3}, {4,5,6}, {7,8,9}}
upperTriangle(arr)
for int i from 0 to arr[0].length() {
for int j from 0 to arr[0].length() {
output(arr[i][j])
}
}

Lower Triangle
lowerTriangle(int arr[][]) {
for i from 1 to arr[0].length {
for j from from 0 to i {
if j < i {
output(arr[i][j])
}
output()
}
}
}

arr[][] = {{1,2,3}, {4,5,6}, {7,8,9}}
lowerTriangle(arr)
for int i from 0 to arr[0].length() {
for int j from 0 to arr[0].length() {
output(arr[i][j])
}
}

Rotate 90
rotate90(int arr[][]){
len = arr[0].length
for i from 0 to len/2 {
for j from i to len - i - 1 {
temp = arr[i][j]
arr[i][j] = arr[len - 1 - j][i]
arr[len - 1 - j][i] = arr[len - 1 - i][len - 1 - j]
arr[len - 1 - i][len - 1 - j] = arr[j][len - 1 - i]
arr[j][len - 1 - i] = temp
}
}
for int i from 0 to arr[0].length() {
for int j from 0 to arr[0].length() {
output(arr[i][j])
}
}
}

arr[][] = {{1,2,3}, {4,5,6}, {7,8,9}}
rotate90(arr)

# data[].attributes.body
Interchange Diagonals:
public static void interchange(int[][] array){
int length = array.length();
for (int i = 0; i < length; i++){
if (i != length / 2)

{

int temp = array[i][i];

array[i][i] = array[i][length - i - 1];

array[i][length - i - 1] = temp;
}
}

Rotate Array:
public static void rotateMatrix(int[][] array){
int length = array.length();

for (int x = 0; x < length / 2; x++) {

for (int y = x; y < length - x - 1; y++) {

int temp = array[x][y];

mat[x][y] = array[y][length - 1 - x];

array[y][length - 1 - x]

= array[length - 1 - x][length - 1 - y];

array[length - 1 - x][length - 1 - y] = array[length - 1 - y][x];

array[length - 1 - y][x] = temp;

}

}

}