borovaneca

MultiplyBigNumbers

Apr 13th, 2023
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.98 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Multiply_Big_Number_05 {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         String number1 = scanner.nextLine();
  8.         String number2 = scanner.nextLine();
  9.  
  10.         if ((number1.charAt(0) == '-' || number2.charAt(0) == '-') && (number1.charAt(0) != '-' || number2.charAt(0) != '-')) {
  11.             System.out.print("-");
  12.         }
  13.  
  14.         if (number1.charAt(0) == '-' && number2.charAt(0) != '-') {
  15.             number1 = number1.substring(1);
  16.         } else if (number1.charAt(0) != '-' && number2.charAt(0) == '-') {
  17.             number2 = number2.substring(1);
  18.         } else if (number1.charAt(0) == '-' && number2.charAt(0) == '-') {
  19.             number1 = number1.substring(1);
  20.             number2 = number2.substring(1);
  21.         }
  22.         System.out.println(multiply(number1, number2));
  23.     }
  24.  
  25.     static String multiply(String num1, String num2) { // Multiplies str1 and str2, and prints result.
  26.         int len1 = num1.length();
  27.         int len2 = num2.length();
  28.         if (len1 == 0 || len2 == 0)
  29.             return "0";
  30.  
  31.         // will keep the result number in vector
  32.         // in reverse order
  33.         int result[] = new int[len1 + len2];
  34.  
  35.         // Below two indexes are used to
  36.         // find positions in result.
  37.         int i_n1 = 0;
  38.         int i_n2 = 0;
  39.  
  40.         // Go from right to left in num1
  41.         for (int i = len1 - 1; i >= 0; i--) {
  42.             int carry = 0;
  43.             int n1 = num1.charAt(i) - '0';
  44.  
  45.             // To shift position to left after every
  46.             // multipliccharAtion of a digit in num2
  47.             i_n2 = 0;
  48.  
  49.             // Go from right to left in num2
  50.             for (int j = len2 - 1; j >= 0; j--) {
  51.                 // Take current digit of second number
  52.                 int n2 = num2.charAt(j) - '0';
  53.  
  54.                 // Multiply with current digit of first number
  55.                 // and add result to previously stored result
  56.                 // charAt current position.
  57.                 int sum = n1 * n2 + result[i_n1 + i_n2] + carry;
  58.  
  59.                 // Carry for next itercharAtion
  60.                 carry = sum / 10;
  61.  
  62.                 // Store result
  63.                 result[i_n1 + i_n2] = sum % 10;
  64.  
  65.                 i_n2++;
  66.             }
  67.  
  68.             // store carry in next cell
  69.             if (carry > 0)
  70.                 result[i_n1 + i_n2] += carry;
  71.  
  72.             // To shift position to left after every
  73.             // multipliccharAtion of a digit in num1.
  74.             i_n1++;
  75.         }
  76.  
  77.         // ignore '0's from the right
  78.         int i = result.length - 1;
  79.         while (i >= 0 && result[i] == 0)
  80.             i--;
  81.  
  82.         // If all were '0's - means either both
  83.         // or one of num1 or num2 were '0'
  84.         if (i == -1)
  85.             return "0";
  86.  
  87.         // genercharAte the result String
  88.         String s = "";
  89.  
  90.         while (i >= 0)
  91.             s += (result[i--]);
  92.  
  93.         return s;
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment