fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main(final String[] args) {
  11. System.out.println("Please enter two numbers which have no more than 50 digits: ");
  12. final Scanner s = new Scanner(System.in);
  13. final String x = s.next();
  14. final String y = s.next();
  15.  
  16. System.out.println("Calculating: (" + x + " + " + y + ")");
  17.  
  18. //The arrays come back in ascending order of magnitude. See method comment.
  19. final int[] xDigits = stringToIntArray(x);
  20. final int[] yDigits = stringToIntArray(y);
  21. final int[] result = new int[51]; //This array will only ever be 51 in size at most
  22. int carryOver = 0;
  23.  
  24. //Add up each individual digit and process any carry over
  25. for (int i = 0; i < 50; i++) {
  26. final int j = xDigits[i] + yDigits[i] + carryOver;
  27. if( j > 9 ) {
  28. result[i] = j % 10;
  29. carryOver = 1; //Since we're adding each individual digit, the only possible carry over is 1.
  30. }
  31. else {
  32. result[i] = j;
  33. carryOver = 0; //No carry over in this case.
  34. }
  35. }
  36.  
  37. //We're now about to print the result, so reverse the array to get the descending order of magnitude
  38. reverseArray(result);
  39.  
  40. System.out.print("Result: ");
  41. //This print boolean is used to ignore any leading 0s.
  42.  
  43. //Use the boolean so that we do print trailing or middling 0s
  44. boolean print = false;
  45. for (final int i : result) {
  46. if( !print && i == 0 ) {
  47. continue;
  48. }
  49. else {
  50. print = true;
  51. }
  52. System.out.print(i);
  53. }
  54. s.close();
  55. }
  56.  
  57. private static void reverseArray(final int[] result) {
  58. for(int i = 0; i < result.length/2; i++) {
  59. final int temp = result[i];
  60. result[i] = result[result.length - i - 1];
  61. result[result.length - i - 1] = temp;
  62. }
  63. }
  64.  
  65. /**
  66. * Will assume the String is a number and return an int[] that contains
  67. * the digits form the number in ascending magnitude order left to write.
  68. * i.e.:
  69. * Input: 12345
  70. * Becomes: {5,4,3,2,1}
  71. */
  72. public static int[] stringToIntArray(final String n) {
  73. final char[] charArray = n.toCharArray();
  74. final int[] result = new int[50];
  75.  
  76. int resultIndex = 0;
  77. for (int i = (charArray.length-1); i >=0 ; i--) {
  78. result[resultIndex] = Integer.parseInt( "" + charArray[i] );
  79. resultIndex++;
  80. }
  81.  
  82. return result;
  83. }
  84. }
Runtime error #stdin #stdout #stderr 0.11s 380672KB
stdin
Standard input is empty
stdout
Please enter two numbers which have no more than 50 digits: 
stderr
Exception in thread "main" java.util.NoSuchElementException
	at java.util.Scanner.throwFor(Scanner.java:907)
	at java.util.Scanner.next(Scanner.java:1416)
	at Ideone.main(Main.java:13)