Array-promo
Matrix-promo

Question

Program to print matrix in spiral order

				
					Matrix

[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

spiral
1 2 3 6 9 8 7 4 5
				
			

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					
import java.util.Scanner;
public class PrintMatrixInSpiralForm
{

    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int arr[][]; 
        int i=0,j=0,top=0,bottom=0,left=0,right=0,row=0,col=0,size=0,count=0;
        System.out.println("ENTER NUMBER OF ROWS");
        row=sc.nextInt();
        System.out.println("ENTER NUMBER OF COLUMNS");
        col=sc.nextInt();
        arr=new int[row][col];
        System.out.println("ENTER ELEMENTS IN ARRAY");
        size=arr.length*arr[0].length;
        count=0;
        for(i=0;i< row;i++)
        {
            for(j=0;j< col;j++)
            {
                arr[i][j]=sc.nextInt();
            }
        }
        System.out.println("ORIGINAL MATRIX");
        for(i=0;i< row;i++)
        {
            for(j=0;j< col;j++)
            {
                System.out.print(arr[i][j]+" ");
            }
            System.out.println();
        }
        
        left=0;
        right=col-1;
        top=0;
        bottom=row-1;
        System.out.println("SPIRAL");
        /*Checking count < size so that loop stops if we have printed all the digits in array*/
        while(count< size)
        {
            /* This for loop prints from left to right*/
            for (i = left; i <= right; i++)
            {
                /*we are checking count < size inside loop to stop printing if we have printed all the digits*/
                if(count< size)
                {
                    System.out.print(arr[top][i] + " ");
                    count++;
                }
            }
            top++;
            /* This for loop prints from up to down*/

            for (i=top; i <= bottom; i++)
            {
                /*we are checking count < size inside loop to stop printing if we have printed all the digits*/
                if(count< size)
                {
                    
                    System.out.print(arr[i][right] + " ");

                    count++;
                }
            }
            right--;
            /* This for loop prints from right to left*/

            for (i = right; i >=left; i--) 
            {
                /*we are checking count < size inside loop to stop printing if we have printed all the digits*/
                if(count< size)
                {
                    System.out.print(arr[bottom][i] + " ");
                    count++;
                }
            }
            bottom--;
            /* This for loop prints from down to up*/

            for (i =bottom ; i >= top; i--)
            {
                /*we are checking count < size inside loop to stop printing if we have printed all the digits*/
                if(count< size)
                {
                    System.out.print(arr[i][left] + " ");
                    count++;
                }
            }
            left++;

        }
    }
}


				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *