Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiplying two long long ints C

I am working on a program in C as a part of Homework in which I have to get the product of two long numbers which are taken as character string. eg: 123456789021 and 132456789098. Since it is taken as a string, I converted them to long long int for the multiplication. But the resulting product will be very large(larger than long long int I guess). Can anyone please suggest me a method to perform this multiplication?

like image 655
Light_handle Avatar asked Dec 06 '09 19:12

Light_handle


2 Answers

Here's one approach: Consider how you would multiply these numbers by hand, on paper. Implement this method in C. You will have to discover:

  • how to break up an integer (represented as a string) into digits
  • how to convert each digit back to an integer 0 <= d < 10
  • how to manage arrays of digits (ie. how big should you make the arrays?)
  • how to write the loop(s) you might need to implement multiplication
  • how to manage carrying products from one digit to the next
  • how to convert those digits back to characters for output
like image 84
Greg Hewgill Avatar answered Oct 04 '22 06:10

Greg Hewgill


usually big integers represented as byte arrays. You can look at Microsoft's BigInteger implementation in DLR. I think they've used algorithms developed by Knuth

like image 27
Ilya Khaprov Avatar answered Oct 04 '22 08:10

Ilya Khaprov