Code written to calculate the product of two arbitrary numbers

Source: Internet
Author: User
package basic;import java.util.Scanner;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Multiple {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubtry {String[] inputstrarry = getTwoNumberInput();int[] iarray1 = changeToIntArray(inputstrarry[0]);int[] iarray2 = changeToIntArray(inputstrarry[1]);int[] iresult = multiplyTwoIntArray(iarray1, iarray2);StringBuffer sresult = new  StringBuffer();for(int i=iresult.length-1;i>=0;i--){if(i==iresult.length-1&&iresult[i]==0){}else{sresult.append(iresult[i]);}}            System.out.println("The result is: "+sresult.toString());} catch (Exception e) {System.err.println(e.getMessage());}}/* * multiplyTwoIntArray */private static int[] multiplyTwoIntArray(int[] iarray1, int[] iarray2) throws Exception{int[] iarray = null;int length1 = iarray1.length;int length2 = iarray2.length;if (iarray1 != null && length1 > 0 && iarray2 != null && length2 > 0) {iarray = new int[length1 + length2];for (int i = 0; i < length1; i++) {for (int j = 0; j < length2; j++) {int itemp = iarray[i + j] + iarray1[i] * iarray2[j];iarray[i + j] = itemp % 10;iarray[i + j + 1] += itemp / 10;if (iarray[i + j + 1] > 10) {iarray[i + j + 1] %= 10;iarray[i + j + 2]++;}}}}if(iarray==null)throw new Exception("Multiply failed!");return iarray;}/* * changeToIntArray */private static int[] changeToIntArray(String str) throws Exception {int[] iarray = null;if (str != null && str.length() > 0) {iarray = new int[str.length()];for (int i = 0; i < str.length(); i++) {iarray[str.length()-i-1] = Integer.parseInt(String.valueOf(str.charAt(i)));}} else {throw new Exception("Error in Change String to int array!");}return iarray;}/* * getTwoNumberInput */private static String[] getTwoNumberInput() throws Exception {String[] ninput = new String[2];Scanner scan = new Scanner(System.in);System.out.println("Please input two number:");String str1 = scan.next();String str2 = scan.next();if (isValidNumber(str1) && isValidNumber(str2)) {ninput[0] = str1;ninput[1] = str2;} else {throw new Exception("Input is invalid!");}return ninput;}private static boolean isValidNumber(String str) {boolean isvalid = false;Pattern p = Pattern.compile("\\d*");Matcher match = p.matcher(str);isvalid = match.matches();return isvalid;}}
 
 

In Java, two integers are multiplied. int and long are limited in size. How can we use biginteger to multiply two integers?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.