You are here:

# C++/multiply big numbers

Question
Hello, David
Thanks for your reply ! I know how to write a program to multiply using float, long, and double data type. I just don't know how to deal with these these big numbers.

-------------------------
Followup To
Question -
I need to multiply signed and unsigned 5o digits numbers. The input has to be from .txt file(not from the key board). Here is an example:
8888888888888888888888888888888888888888888888888
X 77777777777777777777777777777777777777777777777
________________________________________________

Will you please help me how to multiply these numbers either using C/C++. I'm using MS visual C++ 6.0 Thanks, I would really appreciate !
John , Thank you for your question.

To multiply multidigit decimal numbers, you might use the algorithm we all learned in primary school to do manual multiplication. If you don't recall this method in detail, try a Web search.

If you have any problems while writing your program, feel free to ask.

David Spector

John, There is no built-in datatype to represent large numbers. Therefore, C++ has no simple way to do it.

You have to treat this as you would any programming task: design the data layout and the processing necessary to compute the result.

In this case, I recommend that you treat the numbers as arrays of signed characters. Take the ordinary manual multiplication method and implement it on this data layout. For example, your algorithm will start with the least significant (rightmost) digit and multiply it by each digit in the other number, summing and computing carry as it goes.

Be careful to distinguish a character that contains the ISO/ASCII/Unicode representation of a digit (as a printing character), from a binary number representing the actual digit. For example, the digit 0 is 0 in binary, but is represented as 30 (hexadecimal) as a printing character.

David

C++

Volunteer

#### David Spector

##### Expertise

Highly knowledgeable in the C++ language, Visual C++ (MSVC), Windows API, documentation and other quality-assurance techniques, and debugging. Knowledgeable in MFC, COM, GUI design, and object-oriented design.

##### Experience

I have been a software engineer since 1965. I have been published. My specializations have been: biomedical programming, compiler implementation, and many kinds of Windows programming. I don't do Databases or other business-oriented stuff.

Publications
Windows?/DOS Developer's Journal, ACM SIGPLAN Notices, and Computer Science Press.

Education/Credentials
ICCP Systems Programming Certification
Master's degree equivalent in Computer Science