fork download
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. class l1SumDigits
  5. {
  6. public static void main (String[] args) throws java.lang.Exception
  7. {
  8. System.out.println("Enter a number consisting of up to 200 digits:");
  9. Scanner in = new Scanner(System.in);
  10. String input = in.nextLine();
  11. //check whether we got a correct input
  12. if (!input.matches("\\d{1,200}")) {
  13. System.out.println("Incorrect input");
  14. return;
  15. }
  16. int sum = 0;
  17. for (int i=0; i < input.length(); i++) {
  18. sum = sum + Integer.parseInt(String.valueOf(input.charAt(i)));
  19. }
  20. System.out.println("Sum of digits is " + sum);
  21. }
  22. }
Success #stdin #stdout 0.1s 212864KB
stdin
12345678910
stdout
Enter a number consisting of up to 200 digits:
Sum of digits is 46