to add 2 very long integers

V

vicky_in

hi
is anyone tell me any idea about to add 2 integers having more than
20 digits.
e.g. 125865235989523352897956+565896589795623968955632.
 
M

mlimber

vicky_in said:
hi
is anyone tell me any idea about to add 2 integers having more than
20 digits.
e.g. 125865235989523352897956+565896589795623968955632.

Your platform might support built-in or extenstions for integers of
sufficicent size (e.g., long, long long, __int64, __int128, etc.) for
your needs, you could use a floating point representation if the least
significant digits aren't important, or you could use a third-party big
int library that allows for integers of arbitrary length (cf.
http://www.parashift.com/c++-faq-lite/class-libraries.html#faq-37.9).

Cheers! --M
 
P

Phlip

vicky_in said:
is anyone tell me any idea about to add 2 integers having more than
20 digits.
e.g. 125865235989523352897956+565896589795623968955632.

Google "bigint". That's the common name for a class library that manages
arbitrary-size integers.

The answer to the actual question is to express the integer as an array of
ints, where each element is a "place" in counting theory, just like
elementary school arithmetic. Except instead of a 10s place, you have a ~2
billions place. Then to add you detect overflows, and carry to the next
place.
 
O

osmium

vicky_in said:
is anyone tell me any idea about to add 2 integers having more than
20 digits.
e.g. 125865235989523352897956+565896589795623968955632.

If it is a practical problem, there are several libraries that handle big
integers. Lists of libraries are posted occasionally to this group and/or
comp.language.c. You could find a recent list using Google groups.

If it is learning thing, the easiest way is probably to write a BCD adder,
since it minimizes the conversion to/from binary problems.
 
P

Peter Jansson

vicky_in said:
hi
is anyone tell me any idea about to add 2 integers having more than
20 digits.
e.g. 125865235989523352897956+565896589795623968955632.

Hi,

You have two options;

1) Program basic addition between two natural numbers (of arbitrary size).
I guess you know how.

2) Use a third party library that performs the above mentioned addition.

Regards,
Peter Jansson
 
R

Richard Herring

hi
is anyone tell me any idea about to add 2 integers having more than
20 digits.
e.g. 125865235989523352897956+565896589795623968955632.
How would you do it with paper and pencil?

If you write down the rules for that, you are half way to designing a
program that will do it for you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top