Skip to main content

Program in Java: To store values into 3x3 matrix. Find the sum of the boundary of the matrix.

 Program in Java: To store values into 3x3 matrix. Find the sum of the boundary of the matrix. 


/*
 * Program in Java: To store values into 3x3 matrix. Find the sum of the boundary of the matrix. 
   */
import java.util.Scanner;
class boundary
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter 3x3 matrix:");
        int A[][] = new int[3][3];
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {
                A[i][j]=sc.nextInt();
            }
        }
        int ob=0; //outer boundary
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {
                if(i==0 || j==0 || j==2 || i==2)
                    ob=ob+A[i][j];
            }
        }
        System.out.println("Outer boundary sum of matrix:"+ob);
    }
}

Popular posts from this blog