Advertisement
Daniel_007

05. Multiply Big Number

Mar 16th, 2020
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace 05. Multiply Big Number
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string bigNumber = Console.ReadLine();
  12.             int singleDigit = int.Parse(Console.ReadLine());
  13.             string result = string.Empty;
  14.             int number = 0;
  15.             int temp = 0;
  16.             int firstDigit = 0;
  17.             int lastDigit = 0;
  18.             int naum = 0;
  19.             if (singleDigit == 0)
  20.             {
  21.                 Console.WriteLine(0);
  22.                 return;
  23.             }
  24.             for (int i = bigNumber.Length - 1; i >= 0; i--)
  25.             {
  26.                 number = int.Parse(bigNumber[i].ToString());
  27.                 temp = number * singleDigit;
  28.                 lastDigit = temp % 10;
  29.                 if (temp >= 10)
  30.                 {
  31.                     firstDigit = temp / 10;
  32.                 }
  33.                 else
  34.                 {
  35.                     firstDigit = 0;
  36.                 }
  37.                 if (i == bigNumber.Length - 1)
  38.                 {
  39.                     result += lastDigit;
  40.                 }
  41.                 else
  42.                 {
  43.                     int tempResult = naum + lastDigit;
  44.  
  45.                     if (tempResult >= 10)
  46.                     {
  47.                         lastDigit = tempResult % 10;
  48.                         result += lastDigit;
  49.                         firstDigit += tempResult / 10;
  50.                     }
  51.                     else
  52.                     {
  53.                         result += tempResult;
  54.                     }
  55.  
  56.                 }
  57.                 naum = firstDigit;
  58.             }
  59.             if (firstDigit != 0)
  60.             {
  61.                 result += naum;
  62.             }
  63.             string finalString = Reverse(result);
  64.             int lenght = finalString.Length;
  65.             for (int i = 0; i < lenght; i++)
  66.             {
  67.                 char ch = finalString[0];
  68.                 if(ch == '0')
  69.                 {
  70.                     finalString =finalString.Remove(0, 1);
  71.                 }
  72.                 else
  73.                 {
  74.                    
  75.                     break;
  76.                 }
  77.             }
  78.             Console.WriteLine(finalString);
  79.         }
  80.         public static string Reverse(string s)
  81.         {
  82.             char[] charArray = s.ToCharArray();
  83.             Array.Reverse(charArray);
  84.             return new string(charArray);
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement