i have to get 2 numbers (each of length 20 digits) from user and have add them and display the result (using arrays) . i have no idea how to add such a long digit number...please tell me how can i add both numbers.

your quick response will be highly appreciated. Thanks

Recommended Answers

All 11 Replies

Are you asking how to sum the two numbers together or add the two numbers to an array?

sum of two numbers of 20 digits

for example

12345678911234567891+13244321234567898765

Simple example adding two numbers is

int sum_1 = 100;
int sum_2 = 200;
int total;
total = sum_1 + sum_2;//calc both sums = total
cout << "Total = " << total << endl;

I would suggest looking here at c++ operators & the reason is addressing issues & size of types
http://www.cplusplus.com/doc/tutorial/operators/

Simple example adding two numbers is

Clearly you don't understand what the OP is asking. The problem is to add two numbers that cannot be stored in any integer type. It's an exercise in arbitrary precision arithmetic.

You may want to start with a loop, to take in the digits. Make sure you have a way to ensure that, when added, if a digit exceeds 10, it gets added to the higher digit.

Also, remember that arrays begin with [0] and not [1], when you're programming the loop.

Start at the end of each array (ones digit).
Add the two values.
If < 10, store in the last entry of the answer array
If >=10, store (val MOD 10) instead
         save (val / 10) to add to the next digits
Move to the 10's digit and repeat

Try to do it with pen and paper, then write some code for the process you used using arrays.

Hey in java, u will need to accept input in character form then convert it to string n then only u can convert 20 digit no to integer. there is no direct way 2 accept such long no.

while using Biginteger in java

import java.math.BigInteger;

BigInteger number1 = new BigInteger("12345678901234567890");
BigInteger number2 = new BigInteger("90876543210987654321");

BigInteger addition = number1.add(number2);
System.out.println(addition);

Hi jaina

Please read the whole topic before replying, especally for threads that are 6 years old.
The original question was about solving the problem using arrays in C++. While what you post is correct, it's (a) 6 years too late and (b) irrelevant.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.