Advertisement
IrinaIgnatova

Text Processing - Multiply BigNumber

Jul 23rd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.company;
  2.  
  3.  
  4. import java.util.*;
  5. import java.util.stream.Collectors;
  6.  
  7. public class Main {
  8.  
  9.     public static void main(String[] args) {
  10.  
  11.         Scanner scanner = new Scanner(System.in);
  12.  
  13.         String firstNumber = scanner.nextLine();
  14.         int secondNumber = Integer.parseInt(scanner.nextLine());
  15.  
  16.         int result = 0;
  17.         int charAsDigit = 0;
  18.  
  19.         StringBuilder multiplied = new StringBuilder();
  20.  
  21.         for (int i = firstNumber.length() - 1; i >= 0; i--) {
  22.             char currentDigit = firstNumber.charAt(i);
  23.             charAsDigit = currentDigit - '0';
  24.  
  25.             result += charAsDigit * secondNumber;//9*9=81
  26.             if (result > 9) {
  27.                 if (i == 0) {
  28.                     int remainder = result % 10;
  29.                     int leftDigit = (result - remainder) / 10;
  30.                     multiplied.append(remainder);
  31.                     multiplied.append(leftDigit);
  32.  
  33.                 } else {
  34.                     int remainder = result % 10;
  35.                     multiplied.append(remainder);
  36.                     int leftDigit = (result - remainder) / 10;
  37.  
  38.                     result = leftDigit;
  39.                 }
  40.             } else {
  41.                 multiplied.append(result);
  42.                 result = 0;
  43.             }
  44.  
  45.  
  46.         }
  47.  
  48.         System.out.println(multiplied.reverse());
  49.  
  50.  
  51.     }
  52.  
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement