Write a program in Java to create a two-dimensional array of size [n × n].

The value of n is entered by the user and make sure that 2 < n < 10.

Now fill the natural numbers into this matrix in a circular or spiral fashion starting from 1 to n^{2} as illustrated below:

INPUT:

N = 5

OUTPUT:

21 20 19 18 17 22 7 6 5 16 23 8 1 4 15 24 9 2 3 14 25 10 11 12 13

```
import java.io.*;
class Spiral{
public static void main(String args[])throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Matrix size: ");
int n = Integer.parseInt(br.readLine());
if(n < 3 || n > 9){
System.out.println("Size out of range!");
return;
}
int a[][] = new int[n][n];
int value = n * n;
int r1 = n - 1;
int r2 = 0;
int c1 = 0;
int c2 = n - 1;
while(value >= 1){
for(int i = r1; i >= r2; i--)
a[i][c1] = value--;
for(int i = c1 + 1; i <= c2; i++)
a[r2][i] = value--;
for(int i = r2 + 1; i <= r1; i++)
a[i][c2] = value--;
for(int i = c2 - 1; i >= c1 + 1; i--)
a[r1][i] = value--;
r1--;
r2++;
c1++;
c2--;
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
System.out.print(a[i][j] + "\t");
}
System.out.println();
}
}
}
```

## 10 replies on “Spiral Matrix Anticlockwise in Java”

Sir,a program is given which states that:

An interface Shape is defined with a method area() which returns the area of the implementing shape. Create the classes Circle and Rectangle which implement the interface Shape. These classes have attributes which reflect their dimensions (radius for a circle, height and width for a rectangle) which are set by their constructors. The details of the members of the interface and both the classes are given below:

Interface name : Shape Member functions/methods:

double area() : returns the area of the implementing shape

Class name : Circle

Data members/instance variables:

radius : to store radius of the circle in decimal

Member functions/methods: Circle(int r) : parameterized constructor to initiallize radius=r

double area() : to calculate area of the circle [ area of a circle is 3.14*radius*radius]

Class name : Rectangle

Data members/instance variables: length : to store length of the rectangle in decimal breadth : to store breadth of the rectangle in decimal

Member functions / methods Rectangle(int l, int b) : parameterized constructor to initialize length=l, breadth=b double area() : to calculate area of the rectangle [ area of a rectangle is length*breadth]

Assume that the Interface Shape has been defined. Using the concept of inheritance, specify the classes Circle and Rectangle giving details of their constructors and double area( ) respectively.

The interface, main function and algorithm need NOT be written.

Here is the link to the solution for implementing the Shape interface. Just change the data types to int wherever necessary because I have worked with double data types.

Sir,a program is given which states that:

WAP in Java to accept a number and count the number of digits present in that number by using recursive function.

Sample Input:

N=342

Sample Output:

Frequency of digits:3

Follow this page to find out how to count the number of digits.

Sir,how to download microsoft office 2010 in windows 10 pro operating system in 64 bit?From which website can I download office 2010?

I use genuine Microsoft Office. It can be downloaded from Microsoft website. But it is not available for free.

Sir,how to install microsoft office 2010 in windows 10 pro operating system in 64 bit?

Just start the setup and follow the instructions.

Sir,a program is given which states that:

WAP in Java to accept a natural number. Find & print its factors. Use a recursive function that prints the factors of the number.

Visit this page to get access to the recursive method to display the factors of a given number.