1. Write an interactive program that adds two integers of up to 50 digits each (Represents integer as an array of digits).
in Java it would be something like: public static main void(String args[]) { int[] a,b,c; //Suppose a and b store digits for (int i=0;i<a.length;i++) { c[i]=a[i]+b[i]; } }
First, you will have to ask the user for two integers of up to 50 digits each. Then read the input char by char inputting the digits into your array until the number ends. Then repeat for second number. Next you need a way to add the two arrays, which is not as simple as the code above suggests. If the two operands sum to more than nine, you will need to "carry the one". (This also means that an array with a max size of 50 may not be sufficient for an answer array).
I did a project like this in assembly language. It is a lot more work than it sounds (especially in assembly, EVERYTHING is more work than it sounds :P ). As mentioned before, you have to manually check and account for carries when you are adding both arrays. On top of that, you have to account for arrays that are of different sizes. A simple sum array that adds the same index of both arrays together will not do. One way to do it is to normalize the arrays to line up on the ones spot, so for example if the first number was 5 digits and the second was 4, the second number would have a leading 0 added and all the other digits shifted over one. Make sure to keep track of how big each array is. You have to do it manually, since the size of the entire array is not what you are looking for. You can then add them together, but again you have to keep track of whether or not there is a carry. If the digit ends up being greater than or equal to 10, set a Boolean variable to keep track of it and subtract 10 from the digit. If it is less than 10, set the Boolean to false. A simple if(carry){ digit++; } then takes care of the carry (goes before the carry check, the carry check goes at the end of the loop). The other way is to have two separate indices during your add loop, starting at the end of each array and going until one of the arrays is exhausted, then adding the other array by itself (unless there's a carry) into the answer array. I think the first method is much simpler. Another thing to think about is in what order the numbers go in. They are entered from most significant to least significant. If you store them in order as input, you will then have to rearrange them or have your loop work backwards in the array. That makes the addition easier, but then if you want to print out the number or use it somewhere, you will have to work backwards at that point, or rearrange it again. There are many ways to handle all of this. As for a carry in the final add, which can lead to a 51st digit, you can either make your answer array 51 instead of 50 and add 1 to every index as you're adding the arrays into it, then add a 1 into the first spot if there is a carry in the last add, or you can simply check if the Boolean flag is still set to true once you're out of the adding loop and print a leading 1 before you print out the digits, depending on what you want to do with the number.
To go further, you can also include a decimal point, and have the option to subtract.
Join our real-time social learning platform and learn together with your friends!