Java Program to Multiply Corresponding Elements of Two Lists

You are currently viewing Java Program to Multiply Corresponding Elements of Two Lists

Java program to multiply corresponding elements of two lists

import java.util.Arrays;

public class ExArrayMultiplyCorresElem {
  public static void main(String[] args) {
    // take a default string array you wants.
    String result = "";
    int[] left_array = {
      2,
      -5,
      4,
      -2
    };
    int[] right_array = {
      6,
      4,
      -5,
      -2
    };

    // print both the string array first.
    System.out.print("\nArray1: " + Arrays.toString(left_array));
    System.out.println("\nArray2: " + Arrays.toString(right_array));
    for (int i = 0; i < left_array.length; i++) {
      int num1 = left_array[i];
      int num2 = right_array[i];

      // this will calculate the product of the string array.
      result += Integer.toString(num1 * num2) + " ";
    }
    // print the result.
    System.out.println("\nResult: " + result);
  }
}

Output

Array1: [2, -5, 4, -2]
Array2: [6, 4, -5, -2]

Result: 12 -20 -20 4 

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply