Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum of all digits for a given Positive Number [closed]

Tags:

java

algorithm

Method return should be like if entered a number, suppose 345, then output should be 3+4+5=12 --> 1+2 = 3. what i am 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));
  }
}
like image 690
ManishS Avatar asked Dec 21 '12 07:12

ManishS


People also ask

How do you find the sum of all numbers formed from a given set of digits?

Sol: Sum of the numbers formed by taking all the given n digits is ( sum of all the n digits ) x (n-1) !

What is the sum of all digits?

What is digit sum? We can obtain the sum of digits by adding the digits of a number by ignoring the place values. So, for example, if we have the number 567 , we can calculate the digit sum as 5 + 6 + 7 , which will give us 18 .

What would be the sum of all the numbers which can be formed by using the digits 1 3 5 7 all at a time and which have no digits repeated?

Hence the correct answer is (C) 16×3!


6 Answers

O(1) Algo for sum Of digits :

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

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

one liner :

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

Alternate implementation :

public int sumDigit(int n){

      int sum = n % 9;
      if(sum == 0){
          if(n > 0)
               return 9;
      }
      return sum;
}
like image 110
Rahul Avatar answered Oct 19 '22 22:10

Rahul


What you are looking for is digital root. So here's a better solution using the formula from wiki page I linked.

Without Recursion: -

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

And, just in case you want (Which I don't think you would), here's a one-liner (Using Recursion): -

public static int compute( int n ) {
    return n < 10 ? n : compute(n % 10 + compute(n / 10));
}
like image 36
Rohit Jain Avatar answered Oct 19 '22 22:10

Rohit Jain


    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... Poon you were not adding the whole digits.. In middle itself u was keep on adding the right most digit.

like image 20
Gaurav Avatar answered Oct 19 '22 23:10

Gaurav


Many wrong answers here. Here's what OP wants:

Method return should be like if entered a number, suppose 345, then output should be 3+4+5=12 --> 1+2 = 3.

This will do the job:

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;
}
like image 40
jlordo Avatar answered Oct 19 '22 23:10

jlordo


I changed your 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;
    }
}

Please note that i moved the declaration of sum inside the method, instead of making it a field.

like image 43
Frank Avatar answered Oct 20 '22 00:10

Frank


IN your code your are not properly returning values to get call for your recursion method.

        if ((MethParam >= 10)){
            return compute(MethParam);
        }else
            return Sum + MethParam;
like image 45
Smit Avatar answered Oct 20 '22 00:10

Smit