# Multiply Large Numbers using Grid Method

Given two large numbers A and B, the task is to find the product of these two numbers using Grid Method.

Examples:

Input: A = 23, B = 15
Output: 345

Input: A = 321, B = 69
Output: 22149

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

Below is the implementation of the above approach:

 `// Java program to multiply Large ` `// numbers using the grid method ` ` `  `class` `GFG { ` ` `  `    ``// Function to return the multiplication of a and b ` `    ``public` `static` `String multiply(String a, String b) ` `    ``{ ` `        ``boolean` `flag1 = ``false``; ` `        ``boolean` `flag2 = ``false``; ` `        ``a = a.trim(); ` `        ``b = b.trim(); ` ` `  `        ``// To check whether numbers are ` `        ``// negative or positive ` `        ``if` `(a.charAt(``0``) == ``'-'``) { ` `            ``a = a.replace(``"-"``, ``""``); ` `            ``flag1 = ``true``; ` `        ``} ` `        ``if` `(b.charAt(``0``) == ``'-'``) { ` `            ``b = b.replace(``"-"``, ``""``); ` `            ``flag2 = ``true``; ` `        ``} ` ` `  `        ``// To store the result of ` `        ``// multiplication ` `        ``String out = ``""``; ` ` `  `        ``// To create matrix(Grid) of row * column ` `        ``int` `row = a.length(); ` `        ``int` `column = b.length(); ` `        ``int``[][] c = ``new` `int``[row][column]; ` `        ``for` `(``int` `i = ``0``; i < row; i++) { ` `            ``for` `(``int` `j = ``0``; j < column; j++) { ` `                ``int` `n1 ` `                    ``= Character ` `                          ``.getNumericValue( ` `                              ``a.charAt(i)); ` `                ``int` `n2 ` `                    ``= Character ` `                          ``.getNumericValue( ` `                              ``b.charAt(j)); ` `                ``c[i][j] = n1 * n2; ` `            ``} ` `        ``} ` ` `  `        ``// To create 1D array of (row+column-1) size ` `        ``// which is equal to total number ` `        ``// of diagonal in matrix ` `        ``int``[] sum = ``new` `int``[row + column - ``1``]; ` `        ``int` `m = ``0``; ` ` `  `        ``// To add elements of each diagonals ` `        ``for` `(``int` `i = ``0``; i < row; i++) { ` `            ``int` `k = i; ` `            ``int` `add = ``0``; ` ` `  `            ``for` `(``int` `j = ``0``; j < column && k >= ``0``; j++, k--) { ` `                ``add = add + c[k][j]; ` `            ``} ` `            ``sum[m] = add; ` `            ``m = m + ``1``; ` `        ``} ` `        ``for` `(``int` `k = ``1``; k < column; k++) { ` `            ``int` `i = row - ``1``; ` `            ``int` `j = k; ` `            ``int` `add = ``0``; ` `            ``while` `(j < column && i >= ``0``) { ` `                ``add = add + c[i][j]; ` `                ``j = j + ``1``; ` `                ``i = i - ``1``; ` `            ``} ` `            ``sum[m] = add; ` `            ``m = m + ``1``; ` `        ``} ` ` `  `        ``// To check both numbers are not ` `        ``// single digit number ` `        ``if` `(sum.length != ``1``) { ` ` `  `            ``String temp ` `                ``= Integer ` `                      ``.toString( ` `                          ``sum[sum.length - ``1``]); ` `            ``int` `t = ``0``; ` ` `  `            ``// Repeat element in "sum" Array ` `            ``// in reverse order ` `            ``for` `(``int` `n = sum.length - ``1``; n >= ``1``; n--) { ` ` `  `                ``// Add element with result "t" ` `                ``t = t + sum[n]; ` ` `  `                ``// Convert integer element into String ` `                ``// which is sum of all elements ` `                ``// of particular diagonal ` `                ``temp = Integer.toString(t); ` `                ``if` `(temp.length() > ``1``) { ` ` `  `                    ``// If the number contains more than a single-digit ` `                    ``// then copy all the digit into "temp" ` `                    ``// as String except for the unit place digit ` `                    ``String str = temp.substring(``0``, temp.length() - ``1``); ` `                    ``t = Integer.parseInt(str); ` `                ``} ` `                ``else` `{ ` `                    ``t = ``0``; ` `                ``} ` ` `  `                ``// Concat unit place digit at the ` `                ``// beginning of String "out" ` `                ``out = temp.charAt(temp.length() - ``1``) + out; ` `            ``} ` ` `  `            ``// Add first element with result "t" ` `            ``t = t + sum[``0``]; ` `            ``temp = Integer.toString(t); ` `            ``out = temp + out; ` `        ``} ` `        ``else` `{ ` `            ``out = out + sum[``0``]; ` `        ``} ` ` `  `        ``StringBuffer s = ``new` `StringBuffer(out); ` ` `  `        ``// To remove Zero's from the beginning ` `        ``// of the multiplication result ` `        ``for` `(``int` `i = ``0``; i < s.length() - ``1``; i++) { ` `            ``if` `(s.charAt(i) == ``'0'``) { ` `                ``s.deleteCharAt(i); ` `                ``i = i - ``1``; ` `            ``} ` `            ``else` `{ ` `                ``break``; ` `            ``} ` `        ``} ` `        ``out = s.toString(); ` ` `  `        ``// Check if the result of multiplication ` `        ``// operation is zero ` `        ``if` `(!out.equals(``"0"``)) { ` ` `  `            ``// If one of two numbers is negative then ` `            ``// assign minus sign to the result of ` `            ``// multiplication operation ` `            ``if` `(flag1 == ``true` `&& flag2 == ``false``) { ` `                ``out = ``"-"` `+ out; ` `            ``} ` `            ``else` `if` `(flag2 == ``true` `&& flag1 == ``false``) { ` `                ``out = ``"-"` `+ out; ` `            ``} ` `        ``} ` `        ``return` `out; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``String str1 = ``"123456789"``; ` `        ``String str2 = ``"987654321"``; ` `        ``System.out.println(multiply(str1, str2)); ` ` `  `        ``str1 = ``"1235421415454545454545454544"``; ` `        ``str2 = ``"1714546546546545454544548544544545"``; ` `        ``System.out.println(multiply(str1, str2)); ` `    ``} ` `} `

Output:

121932631112635269
2118187521397235888154583183918321221520083884298838480662480

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

7

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.