0

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

9
Contributors
11
Replies
19
Views
6 Years
Discussion Span
Last Post by JamesCherrill
Featured Replies
  • 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. Read More

0

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.

Edited by Red Goose: n/a

0
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
0

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.

0

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);

1

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.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.