Sunday, 6 July 2014

Printing boundary of a matrix

import java.io.*;

class boundary
{
    public static void main(String args[]) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int r,c;
        System.out.println("Enter row:");
        r=Integer.parseInt(br.readLine());
        System.out.println("Enter column:");
        c=Integer.parseInt(br.readLine());
        int[][] ar=new int[r][c];
        System.out.println("Enter value in array:");
        for(int i=0; i<r; i++)
        {
            for(int j=0; j<r; j++)
            {
                ar[i][j]=Integer.parseInt(br.readLine());
            }
        }
        for(int i=0; i<r; i++)
        {
            for(int j=0; j<r; j++)
            {
                System.out.print(ar[i][j] +" ");
            }
               System.out.println("\n");
        }
        for(int i=0; i<r; i++)
        {
            for(int j=0; j<r; j++)
            {
                if(i==0||j==0)
                {
                    System.out.print(ar[i][j] +"  ");
                }
                else if(i==r-1 || j==c-1)
                {

                    System.out.print(ar[i][j] +"  ");
                }
                else
                {
                    System.out.print("\t\t");
                }
            }System.out.println();
        }
    }
}

No comments:

Post a Comment