how to print in a pattern in java

how to print in a pattern in java

how to print this pattern? 1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7

View Answers

October 20, 2012 at 4:25 PM

Here is a code that displays the following pattern:

 1  2  3  4
 12 13 14 5
 11 16 15 6
 10 9  8  7

public class spiral{
    public static void main(String[] args) {
        int n=4;
        int a[][]=new int[n][n];
        int i,j,x=1,p=0,q=n-1;
        while(x<=(n*n))
        {
            for(i=p;i<=q;i++)
            {
                a[p][i]=x;
                x++;
            }
            for(i=(p+1);i<=q;i++)
            {
                a[i][q]=x;
                x++;
            }
            for(i=(q-1);i>=p;i--)
            {
                a[q][i]=x;
                x++;
            }
            for(i=(q-1);i>=(p+1);i--)
            {
                a[i][p]=x;
                x++;
            }
            p++;
            q--;
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                System.out.print(a[i][j]+" ");
            }
            System.out.println();
        }
    }
}









Related Tutorials/Questions & Answers:
How to print the following pattern in java?
How to print the following pattern in java?  How to print the following pattern in java? he he is he is going he is going in   import java.io.*; class StringPattern { public static void main(String[] args
how to print String as pattern ?
how to print String as pattern ?  i have a one String name ="rajesh" i want to print pattern like r * ra ** raj raje rajes rajesh please help me
Advertisements
print rectangle pattern in java
print rectangle pattern in java  * * * * * * * how to generate this pattern in java??   Hi friend try this code may this will helpful for you public class PrintRectangle { public static void main
how to print the following pattern
how to print the following pattern  Hello hEllo heLlo helLo hellO pls reply soon its an emergency   class StringPattern { public static void main(String[] args) { String st="hello
How to print this in java?
How to print pattern in Java?  How to print a particular pattern in Java...;  How to print this in java
print the below pattern
print the below pattern  to write a program using a function ti initialize a character 'A' and print the following pattern of characters upto six lines A A B A B C A B C D A B C D E A B C D E F
how to print - Java Beginners
how to print  how to print something on console without using System.out.print() method ?  Hi Friend, You can use PrintWriter to write anything on the console. import java.io.*; public class Print{ public static
Pattern
Pattern  How to print this pattern in Java please help me A AC ACE ACEG ACEGI The program should be right to left
How to pretty print XML from Java?
How to pretty print XML from Java?  How to pretty print XML from Java
pattern java example
pattern java example  how to print this 1 2 6 3 7 10 4 8 11 13 5 9 12 14 15
how to print from right to left in java????
how to print from right to left in java????   can anyone pls tell how to print from right to left in java
how to make this pattern???
how to make this pattern???  how to make following pattern in javascript
Java pattern
Java pattern  Write a Java program that outputs the following pattern. 1 21 321 4321 54321 654321
Pattern Making
Pattern Making  how to print this pattern in java? * * * * * * * * * * * * * * * * * It is just an example
how to write a program in java to print numbers in equalateral triangle
how to write a program in java to print numbers in equalateral triangle  the output must be 1 324 76589   Here is an example of pattern 1 2 3 4 5 6 7 8 9 Example: public class NumberTriangle{ public
pattern
and a pattern size only between 2 and 9. You are print the menu and the user's response...pattern  write a java program that can create four different patterns of different sizes. The size of each pattern is determined by the number
java pattern
java pattern  12345 23451 34512 45123 41234 12345
how to print the server time
how to print the server time  how to print the server time in jsp and update time in fix interval
How to print this Format exactly?
How to print this Format exactly?   * * * & reverse
How to change the cloth pattern, change the cloth pattern, cloth pattern
How to change the cloth pattern       We are going to change the cloth pattern by using a simple way... Halftone Pattern. ADS_TO_REPLACE_2 Now change layer settings. You will get
how to print the server time
how to print the server time  i want to print database time in clintside and update time in every secound without creating connection to database for each time. how can i do
singleton pattern - Java Beginners
singleton pattern  how i use singleton pattern in my web aplication project.plz give an example.its urgent
Java Singleton Pattern
In this section, you will learn about singleton pattern in Java
code for java pattern
code for java pattern   * * * * * * * * please write the program of above pattern and do not use string tokennizer string buffer i mean to say that please use very simple method Thanks
how to print pdf format
how to print pdf format  Hi every body iam doing school project iam using backend as oracle front end java .how to print student marks list /attendence in pdf format. please help me. thanks in advance.   Here
how to print crystal report from java - Java Beginners
how to print crystal report from java  I want to know -way to print crystal report from java? -which jar are needed to import? I am new at java... for Java: * Expedite data connectivity and formatting using an intuitive design
How to print UIButton id
How to print UIButton id  How to get the id of the button in the IOS application? Thanks   Hi, You can use the following code: - (IBAction)btnClicked:(id)sender { UIButton *button = (UIButton *)sender; int
code for java pattern
code for java pattern  1 23 345 5678 891011 please write the program of above pattern and do not use string tokennizer string buffer i mean to say that please use very simple method Thanks
code for java pattern
code for java pattern  1 121 2342 45674 6789106 please write the program of above pattern and do not use string tokennizer string buffer i mean to say that please use very simple method Thanks
How to print different Messge for the same NumberFormatException on different cause objects in JAVA?
How to print different Messge for the same NumberFormatException on different cause objects in JAVA?  How to print different Messge for the same NumberFormatException on different cause objects in JAVA? try { int
How to design a foot print on the sand, print on the sand, print
How to design a foot print on the sand       You might have seen foot prints on the sand field. Do you think, it can be design in the photoshop, yes I have done here. I have used
Print Form - Java Beginners
Print Form  Hello Sir I have Created Admission Form when user fills data and submit that data to access database,then when i Click on PRINT Button I want to get Print of that forms Contents,How I can Do it with JAVA SWING,plz
print
print  How to print JFrame All Componant?   Please visit the following link: http://www.roseindia.net/java/example/java/swing/Print.shtml
how to print JInternal frame component ?
how to print JInternal frame component ?  hello sir, i devalop a swing application .but problem is that how display report & print it. some data prefech from database & keep to jtable but how it is print with table
Pattern
Pattern  1234321 123 321 12 21 1 1 please,give me this pattern in java   class Sixth_1 { public static void main(String args...; } } }   Here is a number pattern class Pattern{ public
how to print spark dataframe data
how to print spark dataframe data  Hi, I have a dataframe in spark and i want to print all the data on console. How it can be done. how to print... is in "data" variable and you want to print it. Its simple and one line function to print
how to print spark dataframe data
how to print spark dataframe data  Hi, I have a dataframe in spark and i want to print all the data on console. How it can be done. how to print... is in "data" variable and you want to print it. Its simple and one line function to print
Moire Pattern Java Program
Moire Pattern Java Program  **strong text**What you are seeing in the screenshot are two sets of concentric circles. The centers of the circles are 100 pixels apart. The circles have a width of 5, and the diameter of a circle
Moire Pattern 2 Java
Moire Pattern 2 Java   textWhat you are seeing in the screenshot are two sets of concentric circles. The centers of the circles are 100 pixels apart. The circles have a width of 5, and the diameter of a circle is 20 pixels larger
pattern
pattern  1 3 1 5 3 1 7 5 3 1 9 7 5 3   Here is a number pattern example. 1 3 1 5 3 1 7 5 3 1 9 7 5 3 1 class Pattern{ public static void main(String args[]){ for(int i=1;i<=9;i
Print in a long paper - Java Beginners
Print in a long paper  how to print text in long paper?? each print text, printer stops. not continue until the paper print out. Thanks
How to print like printf in python
How to print like printf in python  Hi, I am from C background and there used to be printf function. Jow to print like printf in python? The printf function is C/C++ is very useful. So, looking for some line printf in Python
How can print integer in method
How can print integer in method   a method call process public process(int jobid,int remain){ } how to print int jobid ,int remain in for loop jobid in proc[0],remain in proc[1],both show in text file how to print proc[0
Design pattern - Java Beginners
Design pattern  How to make a java program to check the java program is already started if started the execution then dont allow the new program to start
How to print current time in r?
How to print current time in r?  Hi, I am learning and want to print current date and time on console. How to print current time in R Programming? Thanks   Hi, You should use Sys.time() for getting current date
Java Print Dialog
Java Print Dialog  Using java.awt.print.PrinterJob and javax.print.attribute.PrintRequestAttributeSet. I call .printDialog(ps) and the standard print dialog is displayed with options preset to my chosen attributes. Now I can
how to retrieve the url pattern of the requesting servlet?
how to retrieve the url pattern of the requesting servlet?  Hi... servlet1, you are going to servlet2. So in the servlet2 how you will display the url pattern of the servlet1? This is my question. please answer me soon
What is an efficient way to implement a singleton pattern in Java?
What is an efficient way to implement a singleton pattern in Java?  What is an efficient way to implement a singleton pattern in Java
Java Reverse String Pattern
Java Reverse String Pattern In this section, we have displayed a string in reverse pattern. For this,we have specified a string which is first converted into character array and then print it. Then we have reversed the string using
print a rectangle - Java Beginners
print a rectangle  how do I print a rectangleof stars in java using simple while loop?Assuming that the length n width of the rectangle is given.  Hi friend, I am sending running code. import java.io.

Ads