Project Euler #13: Large sum

  • + 0 comments

    Java8 code

    import java.io.*;
    import java.util.*;
    import java.math.BigInteger;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            int testcases = s.nextInt();  // Number of test cases
            BigInteger sum = BigInteger.ZERO;
    
            // Summing up all the large numbers
            for (int i = 0; i < testcases; i++) {
                String num = s.next();  // Read the number as a string
                sum = sum.add(new BigInteger(num));  // Add to the sum using BigInteger
            }
    
            // Convert the sum to a string and print the first 10 digits
            String result = sum.toString();
            System.out.println(result.substring(0, 10));
        }
    }