Home » Java programs » Java ArrayList programs

Java program to multiply corresponding elements of two lists

Here, we are implementing a java program that will multiply corresponding elements of two array lists.
Submitted by IncludeHelp, on December 11, 2017

Given two array lists and we have to multiply its corresponding elements using java program.

Example:

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

    Output:
    Result: 12 -20 -20 4

Program to multiply corresponding elements of two array lists in java

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 





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.