pattern generation

Ads
 

pattern generation

write a program to print pattern shown below

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

View Answers

August 7, 2012 at 4:09 PM

Here is an example of number spiral.

public class NumberSpiral{

public static void main(String[] args) {
    int dimension=5;
    int[][] spiralArray = new int[dimension][dimension];
    int num = (int) Math.ceil((dimension) / 2.0);

    int j;
    int len = dimension;
    int number = 1;

    for (int i = 0; i < num; i++) {
      for (j = 0; j < len; j++) {
        spiralArray[i][i + j] = number++;
      }
      for (j = 1; j < len; j++) {
        spiralArray[i + j][dimension - 1 - i] = number++;
      }
      for (j = len - 2; j > -1; j--) {
        spiralArray[dimension - 1 - i][i + j] = number++;
      }
      for (j = len - 2; j > 0; j--) {
        spiralArray[i + j][i] = number++;
      }
      len -= 2;
    }
    for (int[] row : spiralArray) {
      for (int elem : row) {
        System.out.printf("%3d", elem);
      }
      System.out.println();
    }
  }

}

Ads









Related Tutorials/Questions & Answers:
pattern generation
pattern generation  write a program to print pattern shown below 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9   Here is an example of number spiral. public class NumberSpiral{ public
pattern
of different sizes. The size of each pattern is determined by the number of columns or rows. For example, a pattern of size 5 has 5 columns and 5 rows. Each pattern is made of character $ and a digit, which shows the size. The size must
Advertisements
Pattern
Pattern  How to print this pattern in Java please help me A AC ACE ACEG ACEGI The program should be right to left
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
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
pdf generation.
pdf generation.  i want to generate the data which is stored in mysql data base in pdf format with php. how i will do
Barcode generation
Barcode generation  hi, I am working on a project where there is a alias no. with symbology EAN-13 for generation of barcode... now i have to write the logic for one item no if their are two or more quantities it should
pattern printing
pattern printing  
PDF Generation for unicode characters
PDF Generation for unicode characters  Hi, How a PDF file having unicode characters is generated in JSP
Design pattern
Design pattern  explain factory pattern
Pattern Making
Pattern Making  how to print this pattern in java? * * * * * * * * * * * * * * * * * It is just an example
pattern program
pattern program  can some one plz help me to wright the logic for the following pattern abcdcba abc cba ab ba a a ab ba abc cba abcdcba
Java pattern
Java pattern  Write a Java program that outputs the following pattern. 1 21 321 4321 54321 654321
ModuleNotFoundError: No module named 'template_generation'
ModuleNotFoundError: No module named 'template_generation'  Hi, My... named 'template_generation' How to remove the ModuleNotFoundError: No module named 'template_generation' error? Thanks   Hi
pattern matcher
pattern matcher  i want a number in a specific range in pattern matcher..for example a number should only be in the range of 6-9 digits... = tf.getText(); Pattern pattern = Pattern.compile("\\d{10}"); Matcher matcher
pattern program
pattern program  can any 1 print the following pattern in java .plz give the code.and yes thank u so much for answering my 2 other questions.had i... before.anyways the pattern goes like this abcdcba abc cba ab ba a a ab ba abc abc
Hibernate generation Id to postgreSQL
Hibernate generation Id to postgreSQL  Hi, I'm trying to implement a id generator in Hibernate, but I'm having a problem. Hibernate doesn't seems to reset the counting when the id is generated. For example: 1 - In PostgreSQL I
pattern program
pattern program  13579 1357 135 13 1 pattern program in java   Here is your Example:- package Compare; public class PatternDemo { public static void main(String [] args){ int c=10; for(int i=0;i<
Code for Pattern
Code for Pattern   1 23 345 5678 891011 Please write the code of above program and please do not use in this string tokennizer, string buffer etc i mean to say that please use very simple method
Pattern matching
Pattern matching  String tagvalue = I've above string and I need... Pattern fnamepat = Pattern.compile("fname=\"([^<>]+)\" "); Matcher fmatch... or any string handling concept. I'm trying Pattern fnamepat = Pattern.compile
Coder for Pattern
Coder for Pattern  1 12 123 1234 12345 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   public class PatternExample
pattern
Play framework dynamic form generation
Play framework dynamic form generation  I want to generate dynamic forms using play framework can any one give me some idea about dynamic form generation using play framework
how to make this pattern???
how to make this pattern???  how to make following pattern in javascript
ModuleNotFoundError: No module named 'odoo10-addon-contract-sale-generation'
ModuleNotFoundError: No module named 'odoo10-addon-contract-sale-generation...: ModuleNotFoundError: No module named 'odoo10-addon-contract-sale-generation' How...-generation' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'odoo10-addon-contract-sale-generation'
ModuleNotFoundError: No module named 'odoo10-addon-contract-sale-generation...: ModuleNotFoundError: No module named 'odoo10-addon-contract-sale-generation' How...-generation' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'random-user-generation'
ModuleNotFoundError: No module named 'random-user-generation'  Hi...: No module named 'random-user-generation' How to remove the ModuleNotFoundError: No module named 'random-user-generation' error? Thanks   Hi
ModuleNotFoundError: No module named 'ros-job_generation'
ModuleNotFoundError: No module named 'ros-job_generation'  Hi, My... named 'ros-job_generation' How to remove the ModuleNotFoundError: No module named 'ros-job_generation' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'ros-groovy-job-generation'
ModuleNotFoundError: No module named 'ros-groovy-job-generation'  Hi...: No module named 'ros-groovy-job-generation' How to remove the ModuleNotFoundError: No module named 'ros-groovy-job-generation' error? Thanks  
ModuleNotFoundError: No module named 'ros-job_generation'
ModuleNotFoundError: No module named 'ros-job_generation'  Hi, My... named 'ros-job_generation' How to remove the ModuleNotFoundError: No module named 'ros-job_generation' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'Pattern'
ModuleNotFoundError: No module named 'Pattern'  Hi, My Python... 'Pattern' How to remove the ModuleNotFoundError: No module named 'Pattern... to install padas library. You can install Pattern python with following command

Ads