If your're looking for easy method to print boundary elements,then you are at the right place.....
****************************
HERE IS THE CODE:
****************************
import java.util.*;
class Only_Boundary_Elements
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int i,j,Temp,n=0;
System.out.println("Enter the Number of Rows");
int r=sc.nextInt();
System.out.println("Enter the Number of Columns");
int c=sc.nextInt();
int arr[][]=new int[r][c];
System.out.println("Enter the Elements of Matrix");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
arr[i][j]=sc.nextInt();
}
}
System.out.println("Input Matrix Is :-");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println(" ");
}
System.out.println("OUTPUT:\nORIGINAL MATRIX");
for(int k=0;k<r;k++)
{
for(int l=0;l<c;l++)
{
if(k==0 || k==r-1)
{
System.out.print(arr[k][l]+"\t");
}
else if(l==0 || l==c-1)
{
System.out.print(arr[k][l]+"\t");
}
else
System.out.print(" \t");
}
System.out.println("");
}
}
}
****************************************************************************
Comments
Post a Comment