Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to print a given matrix in Spiral Form.
Following is a Java program to print the spiral form of a given matrix.
Example
public class PrintMatrixInSpiralForm {
public static void main(String args[]){
int a[][]={{1,2,3},{4,5,6},{7,8,9}};
int w = 0;
int x = a.length-1;
int y = 0;
int z = a[0].length-1;
while(w <= x && y <= z){
for (int i = w; i <= z; i++) {
System.out.print(a[w][i] + " ");
}
for (int i = w+1; i <= x; i++) {
System.out.print(a[i][z] + " ");
}
if(w+1 <= x){
for (int i = z-1; i >= y; i--) {
System.out.print(a[x][i] + " ");
}
}
if(y+1 <= z){
for (int i = x-1; i > w; i--) {
System.out.print(a[i][y] + " ");
}
}
w++;
x--;
y++;
z--;
}
}
}
Output
1 2 3 6 9 8 7 4 5
Advertisements