Java Program to Find Product of Sum of First Row and Last Row

In the previous article, we have seen Java Program to Find Difference between Sum of all Rows and Sum of all Columns

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

Java Program to Find Product of Sum of First Row and Last Row

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 row and last row.

Method-1: Java Program to Find Product of Sum of First Row and Last Row 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 row elements and last row elements.
  • Then find the product between sum of first row elements and last row 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 ;
        //firstRowSum and lastRowSum initialized as 0
        int firstRowSum=0;
        int lastRowSum=0;
        

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

        // Loops to calculate the sum of first row elements and last row elements
        for(row=0;row<3;row++)
        {
            for(col=0;col<3;col++)
            {
                //finding the sum of all elements of first row
                if(row==0)
                    firstRowSum = firstRowSum+arr[0][col];
                //finding the sum of all elements of last row
                else if(row==2)
                    lastRowSum = lastRowSum+arr[2][col];
            }   
        }
        
        
        System.out.println("Sum of all the elements of first row: "+firstRowSum);
        System.out.println("Sum of all the elements of last row: "+lastRowSum);
        
        //finding product between sum of first row elements and last row elements
        int product=firstRowSum*lastRowSum;
       
       //Printing the difference between sum of all rows and all columns
        System.out.print("\nProduct between Sum of first row and last row 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 row: 60
Sum of all the elements of last row: 240

Product between Sum of first row and last row elements : 14400

Method-2: Java Program to Find Product of Sum of First Row and Last Row 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 row elements and last row elements.
  • Then find the product between sum of first row elements and last row 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[3][3];
        int row, col ;
        
        //firstRowSum and lastRowSum initialized as 0
        int firstRowSum=0; 
        int lastRowSum=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 row elements and last row elements
        for(row=0;row<3;row++)
        {
            for(col=0;col<3;col++)
            {
                //finding the sum of all elements of first row
                if(row==0)
                    firstRowSum = firstRowSum+arr[0][col];
                //finding the sum of all elements of last row
                else if(row==2)
                    lastRowSum = lastRowSum+arr[2][col];
            }   
        }
        
        
        System.out.println("Sum of all the elements of first row: "+firstRowSum);
        System.out.println("Sum of all the elements of last row: "+lastRowSum);
        
        //finding product between sum of first row elements and last row elements
        int product=firstRowSum*lastRowSum;
       
       //Printing the difference between sum of all rows and all columns
        System.out.print("\nProduct between Sum of first row and last row 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 row: 15
Sum of all the elements of last row: 11

Product between Sum of first row and last row elements : 165

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.

Related Java Programs: