Skip to content

Instantly share code, notes, and snippets.

@thmain
Created May 8, 2016 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/a048df4e53054ce9b24382813ed8af33 to your computer and use it in GitHub Desktop.
Save thmain/a048df4e53054ce9b24382813ed8af33 to your computer and use it in GitHub Desktop.
public class Diagonals {
public static void print(int [][] a){
//print first half
int row =0;
int col;
while(row<a.length){
col =0;
int rowTemp = row;
while(rowTemp>=0){
System.out.print(a[rowTemp][col] + " ");
rowTemp--;
col++;
}
System.out.println();
row++;
}
//print second half
col = 1;
while(col<a.length){
int colTemp = col;
row = a.length-1;
while(colTemp<=a.length-1){
System.out.print(a[row][colTemp] + " ");
row--;
colTemp++;
}
System.out.println();
col++;
}
}
public static void main(String[] args) {
int [][] a = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
print(a);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment