The sum of all digits for this positive number

The method return should be as if it were entering a number, say 345, then the output should be 3 + 4 + 5 = 12 → 1 + 2 = 3. What am I doing wrong here?

public class DigitSum { int Sum=0; public int compute( int MethParam ) { int rem = MethParam%10; Sum+=rem; MethParam = MethParam/10; if(MethParam>10) compute(MethParam); return Sum+MethParam; } public static void main(String[] args) { DigitSum ds = new DigitSum(); System.out.println(ds.compute(435)); } } 
+6
source share
13 answers

O (1) Algo for the sum of digits:

Taking modulo 9 of any number will return the sum of the digits of that number until a single-digit number is received.

If the number is a multiple of 9, then the sum will be 9

one insert:

 public int sumDigit(int n){ return (n%9 == 0 && n != 0) ? 9 : n%9; } 

Alternative implementation:

 public int sumDigit(int n){ int sum = n % 9; if(sum == 0){ if(n > 0) return 9; } return sum; } 
+27
source

What you are looking for is the digital root . So here is the best solution using the formula from the wiki page I am linked to.

Without recursion:

 public static int compute( int n ) { return n - 9 * ((n - 1) / 9); } 

And, just in case, if you want (that I don’t think you would do it), here is one-line (using recursion) : -

 public static int compute( int n ) { return n < 10 ? n : compute(n % 10 + compute(n / 10)); } 
+8
source
  public int FindSumDigit(int number) { if (number < 10) return number; int sum = 0; while (number > 0) { sum += number % 10; number = number / 10; } return FindSumDigit(sum); } 

Find my code ... Pun, you did not add whole numbers. In the very middle, u continued to add the correct number.

+7
source

There are many incorrect answers. Here is what the OP wants:

The method return should be as if entering a number, say 345, then the output should be 3 + 4 + 5 = 12 → 1 + 2 = 3.

This will complete the task:

 public static int compute(int param) { int sum = 0; do { sum = 0; while (param > 0) { sum += param % 10; param /= 10; } param = sum; } while (sum >= 10); return sum; } 
+3
source

I changed my method to this, then it gives the requested result:

 public int compute(int methParam) { int sum = 0; for (int i = 0; methParam > 10; i++) { int currentDigit = methParam % 10; methParam = methParam / 10; sum = sum + currentDigit; } if (sum + methParam > 10) { return compute(sum + methParam); } else { return sum + methParam; } } 

Note that I moved the sum declaration inside the method instead of making it a field.

+1
source

In your code, you are returning values ​​incorrectly to get a call for your recursion method.

  if ((MethParam >= 10)){ return compute(MethParam); }else return Sum + MethParam; 
+1
source
 public int compute( int param ) { int x = param % 10; int y = param / 10; if (y > 0) return x + compute(y); return x; } public int computeNonRec(int param) { int result = 0; while (param > 0) { result += param % 10; param /= 10; } return result; } 
0
source

Try

  public int sumDigit(int n) { int sum = 0; while (n > 0) { sum += n % 10; number = n / 10; } return sum; } 
0
source

Here's a string solution:

 public int compute( int MethParam ) { int sum = 0; string meth = MethParam.ToString(); for (char x in meth) { sum += int.Parse(x); } if (sum >= 10) { return compute(sum); } else { return sum; } } 

This code is in C #, not Java, so consider it as pseudo code.

0
source

just a different approach, perhaps not as effective due to the transformations involved:

 private static int getUltimateSum(int input) { String inputStr = String.valueOf(input); int sum = 0; for (int i = 0; i < inputStr.length(); i++) { int digit = Integer.valueOf(String.valueOf(inputStr.charAt(i))); sum += digit; } if(sum > 9) return getUltimateSum(sum); else return sum; } 
0
source

to try

 public class DigitSum { int Sum=0; public int compute( int MethParam ) { int rem = MethParam%10; Sum+=rem; MethParam = MethParam/10; if(MethParam>10) compute(MethParam); else Sum+=MethParam; if(Sum>=10){ int temp=Sum; Sum=0; compute(temp); } return Sum; } public static void main(String[] args){ DigitSum ds= new DigitSum(); System.out.println(ds.compute(435)); } } 
0
source

The shortest code for this is

 int compute(int n){ while(n > 9){ n = n - 9; } return n; } 

where n is the number for which you want to calculate some of its digits.

EDIT: effective only for small numbers, such as a three-digit number.

EDIT: Several answers tested:

 public class Test { public static void main(String [] args){ int i = 0x0fefefef; long st1 = System.nanoTime(); int n1 = sumDigit(i); long t1 = System.nanoTime() - st1; long st2 = System.nanoTime(); int n2 = FindSumDigit(i); long t2 = System.nanoTime() - st2; long st3 = System.nanoTime(); int n3 = compute(i); long t3 = System.nanoTime() - st3; long st4 = System.nanoTime(); int n4 = compute1(i); long t4 = System.nanoTime() - st4; System.out.println("Tested for: "+i); System.out.println(n1+": "+t1); System.out.println(n2+": "+t2); System.out.println(n3+": "+t3); System.out.println(n4+": "+t4); } public static int sumDigit(int n){ int sum = n % 9; if(sum == 0){ if(n > 0) return 9; } return sum; } public static int FindSumDigit(int n) { if (n < 10) return n; int sum = 0; while (n > 0) { sum += n % 10; n = n / 10; } return FindSumDigit(sum); } public static int compute( int n ) { return n - 9 * ((n - 1) / 9); } public static int compute1(int n){ while(n > 9){ n = n - 9; } return n; } } 

Here is the result for the test:

 Tested for: 267382767 3: 2432 3: 1621 3: 810 3: 5354519 
0
source

use this simple java code. I created some tymz backwards, this was for adding positive numbers as well as negative numbers:

 class SumDigit { public static void main(String args[]) { int sum, i,a,d; a = Integer.parseInt(args[0]); sum = 0; for(i=1;i< =10;i++) { d = a%10; a = a/10; sum=sum + d; } System.out.println("Sum of Digit :"+sum); } } 
-1
source

All Articles