Java Program to Find Product of Sum of n-th row and n-th column

In the previous article, we have seen Java Program to Find Average of sum of n-th row and n-th column

In this article we are going to see how we can write a program to Find product of Sum of n-th row and n-th column.

Java Program to Find Product of Sum of n-th row and n-th 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=0,  A01=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 n-th row and n-th column.

Method-1: Java Program to Find Product of Sum of n-th row and n-th column By Static Initialization of Array Elements

Approach:

  • Initialize and declare two arrays one of size 3×3 with elements and another 1D array of size 3.
  • Ask the user to enter an index.
  • Take for loops to iterate the row and column index.
  • Find the sum of row elements and column elements of the entered index.
  • Then find the product between sum of n-th row and n-th column.
  • Print the result.

Program:

import java.util.Scanner;
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[][] = {{10,20,30},{40,50,60},{30,60,90}};
        int row, col ;

        System.out.print("The matrix elements are:");
        printMatrix(arr);
        System.out.println("\nEnter nth row-column index to check : ");
        int n = scan.nextInt();
        
        //User defined method called
        //findAverage() method
        findProduct(arr,n);
 
        
    }

    // Function 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]+" ");
            }
        }
    }

    // finding the product between n-th row sum and n-th column sum 
    static void  findProduct(int arr[][], int n)
    {
        int rowSum=0, colSum=0, row, col;
        
        //calculates rowSum
        for(col=0;col<3;col++)
            rowSum+=arr[n][col];
         System.out.println("Row "+n+" sum:"+rowSum);
         
        //calculates colSum
        for(row=0;row<3;row++)
            colSum+=arr[row][n];
        System.out.println("Column "+n+" sum:"+colSum);
        
        //Calculating the product between rowSum and colSum of n-th index
        int product=rowSum*colSum;
        System.out.println("Product between specified row sum and column sum : "+product);
    }
}
Output:

The matrix elements are:
10 20 30 
40 50 60 
30 60 90 
Enter nth row-column index to check : 2
Row 2 sum:180
Column 2 sum:180
Product between specified row sum and column sum : 32400

Method-2: Java Program to Find Product of Sum of n-th row and n-th column By Dynamic Initialization of Array Elements

Approach:

  • Declare two arrays one of size 3×3 and another 1D array of size 3.
  • Ask the user for input of array elements and store them in the one array using two for loops.
  • Ask the user to enter an index.
  • Take for loops to iterate the row and column index.
  • Find the sum of row elements and column elements of the entered index.
  • Then find the product between sum of n-th row and n-th column.
  • Print the result.

Program:

import java.util.Scanner;
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 ;
        
        // 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);
        System.out.println("\nEnter nth row-column index to check : ");
        int n = scan.nextInt();
        
        //User defined method called
        //findAverage() method
        findProduct(arr,n);
 
        
    }

    // Function 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]+" ");
            }
        }
    }

    // finding the product between n-th row sum and n-th column sum 
    static void  findProduct(int arr[][], int n)
    {
        int rowSum=0, colSum=0, row, col;
        
        //calculates rowSum
        for(col=0;col<3;col++)
            rowSum+=arr[n][col];
         System.out.println("Row "+n+" sum:"+rowSum);
         
        //calculates colSum
        for(row=0;row<3;row++)
            colSum+=arr[row][n];
        System.out.println("Column "+n+" sum:"+colSum);
        
        //Calculating the product between rowSum and colSum of n-th index
        int product=rowSum*colSum;
        System.out.println("Product between specified row sum and column sum : "+product);
    }
}
Output:
Enter matrix elements : 10 20 30 40 50 60 70 80 90
The matrix elements are: 2
10 20 30 
40 50 60 
70 80 90 
Enter nth row-column index to check : 
Row 2 sum:240
Column 2 sum:180
Product between specified row sum and column sum : 43200

Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output

Related Java Programs: