Spiral Matrix or Clockwise Circular Matrix - Java

Spiral Matrix or Clockwise Circular Matrix - Java 2021 - ISC Computer Science

Clockwise Circular Matrix or Spiral Matrix in Java
--------------------------------------------------------------------------------------------------------------------

Here is  the Clockwise Circular Matrix or Spiral Matrix in Java - ISC Computer JAVA.

We are uploading these types of program. However, if you need fully compiled solution of any type of Java program then simply leave a comment below, we will help you at the earliest. 😊😊

 import java.util.*;  
 class Clockwise_Circular_Matrix_Or_Spiral_Matrix  
 {  
   public static void main()  
   {  
     Scanner sc=new Scanner(System.in);  
     System.out.print("Enter the Number of Elements:");  
     int n=sc.nextInt();  
     int arr[][]=new int[n][n];  
     int k=1,c1=0,c2=n-1,r1=0,r2=n-1;  
     while(k<=n*n)  
     {  
       for(int i=c1;i<=c2;i++)  
       {  
         // System.out.println("Value"+k);  
         arr[r1][i]=k++;  
       }  
       for(int j=r1+1;j<=r2;j++)  
       {  
         // System.out.println("Value"+k);  
        arr[j][c2]=k++;   
       }  
       for(int i=c2-1;i>=c1;i--)  
       {  
         //  System.out.println("Value"+k);  
         arr[r2][i]=k++;  
       }  
       for(int j=r2-1;j>=r1+1;j--)  
       {  
          //System.out.println("Value"+k);//  
         arr[j][c1]=k++;  
       }  
       c1++;  
       c2--;  
       r1++;  
       r2--;  
     }  
     System.out.println("CIRCULAR MATRIX:");  
     for(int i=0;i<n;i++)  
     {  
       for(int j=0;j<n;j++)  
       {  
         System.out.print(arr[i][j]+"\t");    
       }  
       System.out.println(" ");  
     }  
   }  
 }  
 -------------  
 OUTPUT:-  
 -------------  
 Enter the Number of Elements:4
 CIRCULAR MATRIX:  
 1   2    3  4         
 12  13  14  5            
 11  16  15  6    
 10  9    8  7    

Checkout:- Sort Matrix in Ascending Order Java

Checkout:-  Sorting Rows of Matrix in Descending Order Java

Post a Comment

0 Comments