Results 1 to 15 of 15
 11032011, 08:40 AM #1Member
 Join Date
 Sep 2011
 Posts
 21
 Rep Power
 0
Summing 2 Integers Together (not as easy as it sounds)
Alright, here's the question.
Write a java program to read two integer numbers each up to 30 digits, and then add them and print the result along with input numbers.. Example: Assume the first number is 39983928349458 and the second number is equal to 92387486729, then the output should be as follow.
Output:
39,983,928,349,458+
92,387,486,729

40,076,315,836,187
Note1: You should input your numbers without comma, but the output should be printed with comma as shown in the example.
Note2: You are not allowed to use java's BigInteger, BigDecimal, or similar classes. You should use arrays and create your own algorithm, otherwise, you will not be marked.
Sorry if this sounds like complete BS, I'm not very good with these programming terminologies. Could I get some guidance?
 11032011, 09:05 AM #2
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
Re: Summing 2 Integers Together (not as easy as it sounds)
Step down. Arithmetic calculation and the output representation done separately. Then it will make your life easy. :)
Did you tried anything yet?
 11032011, 09:14 AM #3
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
Re: Summing 2 Integers Together (not as easy as it sounds)
The following simple code segment do the calculation part,
Java Code:import java.util.Scanner; import java.math.BigInteger; class AddNumbers { static Scanner console = new Scanner(System.in); public static void main(String[] args) { System.out.println("1st number; "); BigInteger bigInteger1 = new BigInteger(console.next()); System.out.println("2nd number; "); BigInteger bigInteger2 = new BigInteger(console.next()); BigInteger bigSum = bigInteger1.add(bigInteger2); System.out.println(bigSum); } }
 11032011, 09:32 AM #4
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
Re: Summing 2 Integers Together (not as easy as it sounds)
How funny it is. With the basis stuff I did this in couple of min.
Java Code:import java.util.Scanner; import java.math.BigInteger; import java.text.*; class AddNumbers { static Scanner console = new Scanner(System.in); public static void main(String[] args) { System.out.print("1st number; "); BigInteger bigInteger1 = new BigInteger(console.next()); System.out.print("2nd number; "); BigInteger bigInteger2 = new BigInteger(console.next()); BigInteger bigSum = bigInteger1.add(bigInteger2); DecimalFormat myFormatter = new DecimalFormat("###,###"); String output1 = myFormatter.format(bigInteger1); String output2 = myFormatter.format(bigInteger2); String output3 = myFormatter.format(bigSum); int len1 = output1.length(); int len2 = output2.length(); int max = (len1 > len2) ? len1 : len2; int diff = (len1 > len2) ? (len1  len2) : (len2  len1); String dum = ""; for(int spaces = 0; spaces < diff; spaces++) { dum += " "; } if(len1 < len2) { output1 = dum + output1; } else { output2 = dum + output2; } System.out.println("\n" + output1); System.out.println(output2 + " +"); for(int index = 0; index < max; index++) { System.out.print(""); } System.out.println("\n" + output3); } }
 11032011, 09:46 AM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,820
 Blog Entries
 7
 Rep Power
 21
 11032011, 09:56 AM #6
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
Re: Summing 2 Integers Together (not as easy as it sounds)
Oh I miss it. :S
Still it is possible. By recursive with the integers.
 11032011, 12:35 PM #7
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
Re: Summing 2 Integers Together (not as easy as it sounds)
Bit late, just finish my lunch ;)
Have a look at the following code segment.
Java Code:import java.util.Scanner; class AddNumbers { static Scanner console = new Scanner(System.in); public static void main(String[] args) { // Get the first number from the user System.out.print("1st number; "); String strInput1 = console.next(); // Get the second number from the user System.out.print("2nd number; "); String strInput2 = console.next(); // Identified which one is the lenghty one, not the larger one String max_len = (strInput1.length() > strInput2.length()) ? strInput1 : strInput2; String min_len = (strInput1.length() > strInput2.length()) ? strInput2 : strInput1; // Split into digits and store them in an array String[] digits1 = max_len.split("(?<=.)"); String[] digits2 = min_len.split("(?<=.)"); // Dummy array to format the smallest array above String[] digits3 = new String[digits1.length]; // If the numbers are not in the same size adjust the index on the smallest if(digits1.length > digits2.length) { digits3 = new String[digits1.length]; for(int i = 0; i < digits3.length; i++) { digits3[i] = "0"; } int iCount = digits3.length; for(int j = (digits2.length  1); j >= 0 ; j) { digits3[iCount] = digits2[j]; } } int val = 0; int mod = 0; int up = 0; int low = 0; String sum = ""; // Addition of two numbers for(int index = (digits1.length  1); index >= 0; index) { up = Integer.parseInt(digits1[index]); if(digits1.length > digits2.length) low = Integer.parseInt(digits3[index]); else low = Integer.parseInt(digits2[index]); mod = (up + low + val) % 10; val = (up + low + val) / 10; sum = mod + sum; } // Display the result. Test with any large number :) sum = ((val == 0) ? "" : val) + "" + sum; System.out.println(sum); } }
There are many ways to do the same, and I tried my best to keep the solution simple as possible. If you are not clear with anything please let me know. Good luck! :)
 11032011, 12:42 PM #8Member
 Join Date
 Oct 2011
 Posts
 65
 Rep Power
 0
Re: Summing 2 Integers Together (not as easy as it sounds)
It can write your code with 2 int array
Integer []firstNumber=new Integer[30];
Integer []secondNumber=new Integer[30];
you can use byte array for performance issues.
So as you can see array has 30 digits and each cell (C) ' value must be 0<= C <=9.
You have to use one more thing.
Just fill the array with 1 values;
Ex:
12345
1,2,3,4,5
43210 //Index of Array that corresponds to value
//JUST calculating value . You have to design a little different way
int value=0;
for(int indexOfArrayCell=0;indexOfArrayCell<array.length;i ndexOfArrayCell++){
if(indexOfArrayCell !=1)
value=value+array[indexOfArrayCell]+pow(10,indexOfArrayCell);
}
This is just a small example. You can't use (int value) to get sum. You have to keep those values in array.
 11032011, 12:49 PM #9
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
Re: Summing 2 Integers Together (not as easy as it sounds)
 11032011, 01:29 PM #10Member
 Join Date
 Oct 2011
 Posts
 65
 Rep Power
 0
Re: Summing 2 Integers Together (not as easy as it sounds)
int value can not contain 30 digit number so you can not put value into int
 11032011, 11:26 PM #11Member
 Join Date
 Sep 2011
 Posts
 21
 Rep Power
 0
Re: Summing 2 Integers Together (not as easy as it sounds)
Whoa I didn't expect to see the full written code! It's amazing how things like BigInteger, BigSum, etc...can save a lot of time. Though, I shouldn't use this unless I understand how it works, so I need some clarification.
Could you explain these segments in a bit more detail:
Java Code:String max_len = (strInput1.length() > strInput2.length()) ? strInput1 : strInput2; String min_len = (strInput1.length() > strInput2.length()) ? strInput2 : strInput1; if(digits1.length > digits2.length) { digits3 = new String[digits1.length]; for(int i = 0; i < digits3.length; i++) { digits3[i] = "0"; } int iCount = digits3.length; for(int j = (digits2.length  1); j >= 0 ; j) { digits3[iCount] = digits2[j]; } } if(digits1.length > digits2.length) low = Integer.parseInt(digits3[index]); else low = Integer.parseInt(digits2[index]);
 11072011, 01:36 AM #12Member
 Join Date
 Nov 2011
 Location
 india
 Posts
 1
 Rep Power
 0
Re: Summing 2 Integers Together (not as easy as it sounds)
Why prefer console in place scanner;
 11072011, 01:52 AM #13Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,606
 Rep Power
 12
Re: Summing 2 Integers Together (not as easy as it sounds)
 11072011, 06:24 AM #14
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
Re: Summing 2 Integers Together (not as easy as it sounds)
First of all, sorry for the late reply.
Normally I didn't do this, just pasting the full code relevant to OPs' question. Actually I really hate it. However, your question title gives an impression to give a try.
Of course.
Java Code:String max_len = (strInput1.length() > strInput2.length()) ? strInput1 : strInput2; String min_len = (strInput1.length() > strInput2.length()) ? strInput2 : strInput1;
Java Code:if(digits1.length > digits2.length) { digits3 = new String[digits1.length]; for(int i = 0; i < digits3.length; i++) { digits3[i] = "0"; } int iCount = digits3.length; for(int j = (digits2.length  1); j >= 0 ; j) { digits3[iCount] = digits2[j]; } }
Once I found the longest and shortest numbers, arrange them as follows
XXXXXXXX > Longest number
XXXXX > Shortest number
XXXXXXXX > Longest number
000XXXXX > Shortest number
 11072011, 06:25 AM #15
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
Re: Summing 2 Integers Together (not as easy as it sounds)
Similar Threads

Java: summing up array elements and displaying
By dambundos in forum New To JavaReplies: 7Last Post: 10222011, 02:34 PM 
Summing a Series: Is this correct??
By son012189 in forum New To JavaReplies: 4Last Post: 10052011, 05:36 PM 
Summing the digit
By gozuhair in forum New To JavaReplies: 13Last Post: 07182011, 07:09 AM 
summing value within Hashtable
By dc0m in forum New To JavaReplies: 4Last Post: 09232010, 12:17 AM 
Help with summing series
By xplsivo in forum New To JavaReplies: 8Last Post: 11232009, 08:37 PM
Bookmarks