Multiply numbers represented as arrays in Java?

multiply each element of an array by a number in java
how to multiply each number in an array
java multiply all elements in array
multiplication of two 1d array in c
multiply each element of an array by a number in c
multiplication of array in c++
multiply large numbers represented as strings java
multiply an array

I have to write a function that multiplies two numbers represented by two int arrays (so I can't use ArrayLists or something).

Each digit of a number is represented by an int between 0 and 9 in the array, no element should be greater than that.

The first element of the array represents the last digit of the number and so on, therefore the number 1234 would be {4,3,2,1} as an array in this function.

I thought multiplying those arrays that way would be similar to long multiplication, so I tried to implement it in a similar way: You multiply every digit of the first array with every digit of the second one and store the rest if the result is equal or greater to 10 and then add it to the next digit. However, I seem to have done something wrong in the code (maybe the calculation of the rest??) because the result of my function is not correct: I tested it with 190 times 86 (represented by the arrays {0,9,1} and {6,8}) and get 15342 ({2,4,3,5,1}) instead of the actual result 16340 (which would be {0,4,3,6,1}).

Can somebody here help me out with this please? This is my code:

import java.util.Arrays;
public class MultiplyArrays {

 static int[ ] times(int[ ] a, int[ ] b) {
   int[] arr = new int[a.length + b.length - 1];//arr should be the result of a*b. The result shouldn't be shorter than that
   int tmp = 0;//stores the rest of two-digit numbers
   for(int i = b.length - 1; i >= 0; i--){
     for(int j = 0; j < a.length; j++){//should multiply all digits of a with the current index of b
       arr[i + j] = (arr[i + j] + (b[i] * a[j] + tmp)) % 10;//sets the value of the (i+j)th index in arr to the multiplication of two numbers from a and b adding the rest tmp.
       if((arr[i + j] + b[i] * a[j] + tmp) < 10){//if this number is less than 10, there is no rest
         tmp = 0;
       else{//otherwise, the rest should be the second digit
         tmp = (((arr[i + j] + (b[i] * a[j] + tmp))) - ((arr[i + j] + (b[i] * a[j] + tmp)) % 10)) / 10;//something in the formula for the rest is wrong, I guess
   if(tmp != 0){//if the last number of the array containing the result is calculated and there still is a rest, a new array with one more digit is created
     int[] arr2 = new int[arr.length + 1];
     for(int i = arr.length - 1; i >= 0; i--){//the new array copies all numbers from the old array
       arr2[i] = arr[i];
       arr2[arr2.length - 1] = tmp;//the last space is the rest now
     return arr2;
   else{//if there is no rest after calculating the last number of arr, a new array isn't needed
     return arr;
    public static void main(String[] args) {//test the function with 190 * 86
        int[] a = {0,9,1};
        int[] b = {6,8};


Maybe this comes from the fact that your indices in the for-loops of the times()-method are incrementing AND decrementing. The i is going down and the j is going up. Also, in the second for loop, you should only increment to 'a.length - 1', not to 'a.length'.

Multiply Large Numbers represented as Strings, The approach used in below solution is to keep only one array for result. C++ program to multiply two numbers represented import java.util. Russian Peasant (Multiply two numbers using bitwise operators) Add two numbers represented by two arrays; Add two numbers represented by linked lists | Set 1; Add two numbers represented by linked lists | Set 2; Check if given number can be represented as sum of two great numbers; Sum of two numbers where one number is represented as array of

Arbitrary precision multiplication is more complex than it seems, and contains corner cases (like one and zero). Fortunately, Java has an arbitrary precision type; BigInteger. In order to use it here, you would need to create two additional methods; one for converting an int[] to a BigInteger, and the second the convert a BigInteger to an int[].

The first can be done with a single loop adding each digit at index i (multiplied by 10i) to a running total. Like,

private static BigInteger fromArray(int[] arr) {
    BigInteger bi = BigInteger.ZERO;
    for (int i = 0, pow = 1; i < arr.length; pow *= 10, i++) {
        bi = bi.add(BigInteger.valueOf(arr[i] * pow));
    return bi;

And the second can be done a number of ways, but the easiest is simply to convert the BigInteger to a String to get the length() - once you've done that, you know the length of the output array - and can populate the digits in it. Like,

private static int[] toArray(BigInteger bi) {
    String s = bi.toString();
    int len = s.length();
    int[] r = new int[len];
    for (int i = 0; i < len; i++) {
        r[i] = s.charAt(len - i - 1) - '0';
    return r;

Finally, call those two methods and let BigInteger perform the multiplication. Like,

static int[] times(int[] a, int[] b) {
    BigInteger ba = fromArray(a), bb = fromArray(b);
    return toArray(ba.multiply(bb));

Running your original main with those changes outputs (as expected)

[0, 4, 3, 6, 1]

Program for multiplication of array elements, We traverse array from left to right and multiply elements with result. C++; Java; Python3; C#; PHP. C++. Given two numbers represented by linked lists, write a function that returns the multiplication of these two linked lists. Examples: Input : 9->4->6 8->4 Output : 79464 Input : 3->2->1 1->2 Output : 3852

Java exercises: Multiply corresponding elements of two arrays of , Java programming exercises and solution: Write a Java program to multiply corresponding elements of two arrays of integers. The following Java program demonstrates multiplication of two numbers. This is a trivial example program, however it shows how to handle input and output, performing calculations and the use of library classes such as Scanner in Java language. The user is prompted to enter first and the second number.

Multiply large numbers represented as strings, Java data types. Immutable, arbitrary-precision signed decimal numbers. Multiply Large Numbers represented as Strings; Count the number of subarrays having� Add two numbers represented by two arrays 27-12-2017 Given two array A[0….n-1] and B[0….m-1] of size n and m respectively, representing two numbers such that every element of arrays represent a digit.

Java Program to Multiply Two Numbers, When you start learning java programming, you get these type of problems in your assignment. Here we will see two Java programs, first program takes two. In this article, we will write a program to multiply (or divide) every element of an array with a constant number and print the output. Submitted by Abhishek Pathak , on October 15, 2017 JavaScript is the language of the web, but with Node.js this scenario has changed.

Write a java program multiply, //carry number represented by 2 element String array. String result = "";// result to be stored in string form int resultInt = 0;//carry in form of integer //int count = 0; Efficiently merging two sorted arrays with O(1) extra space and O(NlogN + MlogM) Count of subsets with sum equal to X using Recursion; Split the given string into Primes : Digit DP; Number of pairs in an array with the sum greater than 0; Real-time application of Data Structures; Count of subarrays of an Array having all unique digits

  • Is this a homework question? I'd convert it into a string, reverse it, convert to int, multiply both, then re-convert back. A better way could be to sum each number, increasing by 10**n every number (and looping through array backwards) then multiplying the resulting two integers.