fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4.  
  5. /* Name of the class has to be "Main" only if the class is public. */
  6. class Ideone
  7. {
  8. static int digitSum(int input1){
  9. int newinput,sum=0;
  10. newinput = input1;//just to hold the copy of number so that we can later check if number is negative or not
  11. if(input1<0){
  12. input1 = -1*input1;
  13. }
  14. while(input1>0 || sum>9){
  15. if(input1==0)
  16. {
  17. input1=sum;
  18. sum=0;
  19. }
  20. sum += input1%10;
  21. input1 = input1/10;
  22. }
  23. if(newinput > 0){
  24. return sum;
  25. }
  26. else{
  27. return -1 * sum;
  28. }
  29. }
  30. public static void main (String[] args) throws java.lang.Exception
  31. {
  32. // your code goes here
  33. int input;
  34. Scanner sc = new Scanner(System.in);
  35. input = sc.nextInt();
  36. System.out.println(digitSum(input));
  37. }
  38. }
Success #stdin #stdout 0.13s 49436KB
stdin
-976592
stdout
-2