Posts

Showing posts from August, 2014

Java Program to print Circular (Spiral) Matrix

Image
Question:             Write a Program in Java to fill a square matrix of size ‘n*n” in a circular fashion (clockwise) with natural numbers from 1 to n*n, taking ‘n’ as input. For example: if n = 4, then n*n = 16, hence the array will be filled as given below Note: This program is also known as Spiral Matrix Solution: import java.io.*; class Circular_Matrix     {         public static void main(String args[])throws IOException         {             BufferedReader br=new BufferedReader(new InputStreamReader(System.in));             System.out.print("Enter the number of elements : ");             int n=Integer.parseInt(br.readLine());          ...