Advertisement
Guest User

Untitled

a guest
Dec 31st, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.27 KB | None | 0 0
  1. // C program to multiply two numbers represented
  2. // as strings.
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. // Multiplies str1 and str2, and prints result.
  8. char* multiply(char* num1, char* num2)
  9. {
  10.     int n1 = strlen(num1);
  11.     int n2 = strlen(num2);
  12.     if (n1 == 0 || n2 == 0)
  13.         return "0";
  14.  
  15.     // will keep the result number in vector
  16.     // in reverse order
  17.     int* result = malloc((n1+n2) * sizeof(int));
  18.  
  19.     // Below two indexes are used to find positions
  20.     // in result.
  21.     int i_n1 = 0;
  22.     int i_n2 = 0;
  23.  
  24.     // Go from right to left in num1
  25.     for (int i=n1-1; i>=0; i--)
  26.     {
  27.         int carry = 0;
  28.         int n1 = num1[i] - '0';
  29.  
  30.         // To shift position to left after every
  31.         // multiplication of a digit in num2
  32.         i_n2 = 0;
  33.  
  34.         // Go from right to left in num2
  35.         for (int j=n2-1; j>=0; j--)
  36.         {
  37.             // Take current digit of second number
  38.             int n2 = num2[j] - '0';
  39.  
  40.             // Multiply with current digit of first number
  41.             // and add result to previously stored result
  42.             // at current position.
  43.             int sum = n1*n2 + result[i_n1 + i_n2] + carry;
  44.  
  45.             // Carry for next iteration
  46.             carry = sum/10;
  47.  
  48.             // Store result
  49.             result[i_n1 + i_n2] = sum % 10;
  50.  
  51.             i_n2++;
  52.         }
  53.  
  54.         // store carry in next cell
  55.         if (carry > 0)
  56.             result[i_n1 + i_n2] += carry;
  57.  
  58.         // To shift position to left after every
  59.         // multiplication of a digit in num1.
  60.         i_n1++;
  61.     }
  62.  
  63.     // ignore '0's from the right
  64.     int i = (n1+n2) - 1;
  65.     while (i>=0 && result[i] == 0)
  66.         i--;
  67.  
  68.     // If all were '0's - means either both or
  69.     // one of num1 or num2 were '0'
  70.     if (i == -1)
  71.         return "0";
  72.  
  73.     // generate the result string
  74.     char* s;
  75.     s = malloc(i*sizeof(char));
  76.     char *buffer = malloc(2*sizeof(char));
  77.     while (i >= 0){
  78.         sprintf(buffer,"%d",result[i--]);
  79.         strcat(s,buffer);
  80.     }
  81.  
  82.     return s;
  83.     return "";
  84. }
  85.  
  86. // Driver code
  87. int main()
  88. {
  89.     char* str1 = "1235421415454545454545454544";
  90.     char* str2 = "1714546546546545454544548544544545";
  91.     printf("%s\n",multiply(str1, str2));
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement