write a program in java.
arrange the natural n umber in 5x5 matrix as
21 22 23 24 25
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13
i at centerd position and remaining arrange in anticlockwise direction.
View Answers
August 19, 2011 at 1:28 PM
public class NumberSpiral{
public static void main(String[] args) {
int n=5;
int i, s = n,x=0, y=0;
int arr [][]= new int [n][n];
i=n*n;
while(i>=1){
for(int j=0; j<s; j++)
arr[x][y++] = i--;
x++;
y--;
if(i>=1){for(int j=0; j<s-1; j++)
arr[x++][y] = i--;
x--;
y--;
}
if(i>=1){for(int j=0; j<s-1; j++)
arr[x][y--] = i--;
x--;
y++;
}
if(i>=1){for(int j=0; j<s-2; j++)
arr[x--][y] = i--;
y++;
x++;
s=s-2;
}
}
for(i=0; i<n; i++){
for(int j=0; j<n; j++)
System.out.print(arr[i][j] + "\t");
System.out.println();
}
}
}
Ads
Related Tutorials/Questions & Answers:
Advertisements
Write a java application program........?
Write a
java application
program........? Welcome every One :
I have
Q in
Java?
Q :
Write a
java application
program that reads 20 integer numbers input by the user and prints how many numbers > 100 and how many numbers
Write a program in java...
Write a
program in
java... Hi, friends
Please, can you help me?
Q1:
Write a
program in
java to simulate a calculator. Your
program should take two... to enter an integer number.
Write a
program in
java to find the factorial
WRITE A PROGRAM IN JAVA
WRITE A
PROGRAM IN JAVA
Write program which accepts five students id,name,age,department,courses, taken and its grade.
a. calculate its GPA
. display all student information in ascending order
WRITE A PROGRAM IN JAVA
WRITE A
PROGRAM IN JAVA How do I
write a
program in
Java, have the
program display a message with your name in it and a number (Hello john Smith!1) The number must increment from zero to nine, or decrement fron nine to zero. You
write a program in java.
write a
program in
java. arrange the natural n umber in 5x5 matrix as
21 22 23 24 25
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13
i at centerd position and remaining arrange in anticlockwise direction.
 
write a program
write a program
write a
program add 2 no.s without use arithmetic,unary operaters in
java
Help me to write this simple java frame program
Help me to
write this simple
java frame
program I want to
write a
Java program for....
To create a frame with 4 text fields;name,street,city and pin;with suitable labels.
Also add a button "OK". After enter values
how do i write a java program for this??
how do i
write a
java program for this?? â??Ask the user if they have a dog. If â??yesâ??, ask the user how old is and compute the dogâ??s age... in your
program
how to write this program
how to
write this program
Write a
program in
java which calculates... or numerical grade. You will be submitting TWO versions of this
program.
Write one...?˘s GPA. This
program should take a student�s First Name, Last Name a GPA
write a program to print
write a
program to print
write a
program to print following
1 2 3 4 5 4 3 2 1
1 2 3 4 4 3 2 1
1 2 3 3 2 1
1 2 2 1
1 1