Advertisement
CR7CR7

Myltiplayer

Apr 16th, 2023
1,146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.95 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. // Create a class named MultiplyBigNumber
  4. public class MultiplyBigNumber {
  5.     // Create a main method
  6.     public static void main(String[] args) {
  7.         // Create a Scanner object named scanner
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         // Read a String named number1 from the scanner
  11.         String number1 = scanner.nextLine();
  12.         // Read a String named number2 from the scanner
  13.         String number2 = scanner.nextLine();
  14.  
  15.         // Close the scanner
  16.         scanner.close();
  17.  
  18.         // If one of the numbers is negative and the other is not, print "-" sign
  19.         if ((number1.charAt(0) == '-' || number2.charAt(0) == '-') && (number1.charAt(0) != '-' || number2.charAt(0) != '-')) {
  20.             System.out.print("-");
  21.         }
  22.  
  23.         // If both numbers are negative, remove the "-" sign from both of them
  24.         if (number1.charAt(0) == '-' && number2.charAt(0) == '-') {
  25.             number1 = number1.substring(1);
  26.             number2 = number2.substring(1);
  27.         }
  28.  
  29.         // Call the multiply method with number1 and number2 and print the result
  30.         System.out.println(multiply(number1, number2));
  31.     }
  32.  
  33.     // Create a static method named multiply that takes two Strings as parameters and returns a String
  34.     static String multiply(String num1, String num2) {
  35.         // Get the lengths of the numbers and store them in int variables named len1 and len2
  36.         int len1 = num1.length();
  37.         int len2 = num2.length();
  38.        
  39.         // If either length is 0, return "0"
  40.         if (len1 == 0 || len2 == 0)
  41.             return "0";
  42.  
  43.         // Create an int array named result with size len1 + len2 and fill it with zeros
  44.         int[] result = new int[len1 + len2];
  45.        
  46.         // Create two int variables named i_n1 and i_n2 and assign them to 0
  47.         int i_n1 = 0;
  48.         int i_n2 = 0;
  49.  
  50.         // Loop from the last index of num1 to the first index
  51.         for (int i = len1 - 1; i >= 0; i--) {
  52.             // Create an int variable named carry and assign it to 0
  53.             int carry = 0;
  54.             // Convert the char at the current index of num1 to an int and store it in an int variable named n1
  55.             int n1 = num1.charAt(i) - '0';
  56.  
  57.             // Set i_n2 to 0
  58.             i_n2 = 0;
  59.  
  60.             // Loop from the last index of num2 to the first index
  61.             for (int j = len2 - 1; j >= 0; j--) {
  62.                 // Convert the char at the current index of num2 to an int and store it in an int variable named n2
  63.                 int n2 = num2.charAt(j) - '0';
  64.  
  65.                 // Multiply n1 and n2 and add carry and the value at result[i_n1 + i_n2], store the result in an int variable named sum
  66.                 int sum = n1 * n2 + carry + result[i_n1 + i_n2];
  67.  
  68.                 // Set carry to sum / 10
  69.                 carry = sum / 10;
  70.  
  71.                 // Set result[i_n1 + i_n2] to sum % 10
  72.                 result[i_n1 + i_n2] = sum % 10;
  73.  
  74.                 // Increment i_n2 by 1
  75.                 i_n2++;
  76.             }
  77.  
  78.             // If carry is not 0, add it to result[i_n1 + i_n2]
  79.             if (carry > 0)
  80.                 result[i_n1 + i_n2] += carry;
  81.  
  82.             // Increment i_n1 by 1
  83.             i_n1++;
  84.         }
  85.  
  86.         // Create an int variable named i and assign it to result.length - 1
  87.         int i = result.length - 1;
  88.        
  89.         // While i is greater than or equal to 0 and result[i] is equal to 0, decrement i by 1
  90.         while (i >= 0 && result[i] == 0)
  91.             i--;
  92.  
  93.         // If i is equal to -1, return "0"
  94.         if (i == -1)
  95.             return "0";
  96.  
  97.         // Create a StringBuilder object named sB
  98.         StringBuilder sB = new StringBuilder();
  99.  
  100.         // While i is greater than or equal to 0, append result[i] to sB and decrement i by 1
  101.         while (i >= 0)
  102.             sB.append(result[i--]);
  103.  
  104.         // Return sB as a String
  105.         return sB.toString();
  106.     }
  107. }
  108.      
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement