# Java Program to Find Product of Sum of First Column and Last Column

In this article we are going to see how we can write a program to find product of sum of first column and last column using JAVA language.

## Java Program to Find Product of Sum of First Column and Last Column

A 3*3 Matrix is having 3 rows and 3 columns where this 3*3 represents the dimension of the matrix. Means there are 3*3 i.e. total 9 elements in a 3*3 Matrix.

Let’s understand it in more simpler way.

                   | A00   A01   A02 |
Matrix A =  | A10   A11   A12 |
| A20   A21   A22 | 3*3
• Matrix A represents a 3*3 matrix.
• A‘ represents the matrix element
• Aij‘ represents the matrix element at it’s matrix position/index.
• i‘ represents the row index
• j‘ represents the column index
• Means A00=Aij  where i=0 and j=0A01=aij where i=0 and j=1 and like this.
• Here we have started row value from 0 and column value from 0.

Let’s see different ways to find product of sum of first column and last column.

### Method-1: Java Program to Find Product of Sum of First Column and Last Column By Static Initialization of Array Elements

Approach:

• Initialize and declare one array with size 3×3 which is for matrix.
• Use two for loops to iterate the rows and columns .
• Inside the for loops calculate the sum of first column elements and last column elements.
• Then find the product between sum of first column elements and last column elements.
• Print the result using a for loop.

Program:

public class Matrix
{
public static void main(String args[])
{

// Initializing the 3X3 matrix i.e. 2D array
int arr[][] = {{10,20,30},{40,40,60},{70,80,90}};
int row, col ;
//firstColumnSum and lastColumnSum initialized as 0
int firstColumnSum=0;
int lastColumnSum=0;

System.out.print("The matrix elements are : ");
printMatrix(arr);

// Loops to calculate the sum of first column elements and last column elements
for(row=0;row<3;row++)
{
for(col=0;col<3;col++)
{
//finding the sum of all elements of first row
if(col==0)
firstColumnSum = firstColumnSum+arr[row];
//finding the sum of all elements of last row
else if(col==2)
lastColumnSum = lastColumnSum+arr[row];
}
}

System.out.println("Sum of all the elements of first column: "+firstColumnSum);
System.out.println("Sum of all the elements of last column: "+lastColumnSum);

//finding product between sum of first column elements and last column elements
int product=firstColumnSum*lastColumnSum;

//Printing the difference between sum of all rows and all columns
System.out.print("\nProduct between Sum of first column and last column elements : "+product);

}

//Method to print the matrix
static void printMatrix(int arr[][])
{
int row, col;
// Loop to print the elements
for(row=0;row<3;row++)
{
// Used for formatting
System.out.print("\n");
for(col=0;col<3;col++)
{
System.out.print(arr[row][col]+" ");
}
}
System.out.print("\n");
}
}
Output:

The matrix elements are :
10 20 30
40 40 60
70 80 90
Sum of all the elements of first column: 120
Sum of all the elements of last column: 180

Product between Sum of first column and last column elements : 21600

### Method-2: Java Program to Find Product of Sum of First Column and Last Column By Dynamic Initialization of Array Elements

Approach:

• Declare one array with size 3×3 which is for matrix.
• Ask the user for input of matrix elements.
• Use two for loops to iterate the rows and columns .
• Inside the for loops calculate the sum of first column elements and last column elements.
• Then find the product between sum of first column elements and last column elements.
• Print the result using a for loop.

Program:

import java.util.*;

public class Matrix
{
public static void main(String args[])
{

//Scanner class to take input
Scanner scan = new Scanner(System.in);
// Initializing the 3X3 matrix i.e. 2D array
int arr[][] = new int;
int row, col ;

//firstColumnSum and lastColumnSum initialized as 0
int firstColumnSum=0;
int lastColumnSum=0;

// Taking matrix input
System.out.println("Enter matrix elements : ");
for(row=0;row<3;row++)
for(col=0;col<3;col++)
arr[row][col] = scan.nextInt();

System.out.print("The matrix elements are : ");
printMatrix(arr);

// Loops to calculate the sum of first column elements and last column elements
for(row=0;row<3;row++)
{
for(col=0;col<3;col++)
{
//finding the sum of all elements of first row
if(col==0)
firstColumnSum = firstColumnSum+arr[row];
//finding the sum of all elements of last row
else if(col==2)
lastColumnSum = lastColumnSum+arr[row];
}
}

System.out.println("Sum of all the elements of first column: "+firstColumnSum);
System.out.println("Sum of all the elements of last column: "+lastColumnSum);

//finding product between sum of first column elements and last column elements
int product=firstColumnSum*lastColumnSum;

//Printing the difference between sum of all rows and all columns
System.out.print("\nProduct between Sum of first column and last column elements : "+product);

}

//Method to print the matrix
static void printMatrix(int arr[][])
{
int row, col;
// Loop to print the elements
for(row=0;row<3;row++)
{
// Used for formatting
System.out.print("\n");
for(col=0;col<3;col++)
{
System.out.print(arr[row][col]+" ");
}
}
System.out.print("\n");
}
}
Output:

Enter matrix elements :
The matrix elements are :
9 5 1
7 4 8
2 6 3
Sum of all the elements of first column: 18
Sum of all the elements of last column: 12

Product between Sum of first column and last column elements : 216

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.