How do I write a recursive method to return the sum of digits to an int?

So this is my code.

    public int getsum (int n){
        int num = 23456;
        int total = 0;
        while (num != 0) {
            total += num % 10;
            num /= 10;
        }
    } 

      

The problem is I cant / know how to change this to a recursive method Im kind of new with recursion and I need help implementing this method to change it so that it is recursive.

+3


source to share


12 replies


Short, recursive and does the job:



int getsum(int n) {
   return n == 0 ? 0 : n % 10 + getsum(n/10);
}

      

+13


source


Here he is,

//sumDigits function
int sumDigits(int n, int sum) {    
    // Basic Case to stop the recursion
if (n== 0)  {
        return sum;
    } else {
        sum = sum + n % 10;  //recursive variable to keep the digits sum
        n= n/10;
        return sumDigits(n, sum); //returning sum to print it.
    }
}

      



An example of a function in action:

public static void main(String[] args) {
     int sum = sumDigits(121212, 0);
     System.out.println(sum);
}

      

+6


source


public int sumDigits(int n) {
    return (n - 1) % 9 + 1;
}

      

+2


source


public static int sumOfDigit(int num){
    int sum=0;
    if (num == 0)
    return sum;
            sum = num%10 + sumOfDigit(num/10);
    return sum;
}
public static void main(String args[]) {
    Scanner input=new Scanner(System.in);
    System.out.print("Input num : ");
    int num=input.nextInt();
    int s=sumOfDigit(num);
    System.out.println("Sum = "+s);
}

      

}

+1


source


Try the following:

int getSum(int num)
{
    total = total + num % 10;
    num = num/10;
    if(num == 0)
    {
        return total;
    } else {
        return getSum(num);
    }
}

      

0


source


int getSum(int N)
{
    int totalN = 0;

    totalN += (N% 10);
    N/= 10;

    if(N == 0)
        return totalN;
    else 
        return getSum(N) + totalN;
}

      

0


source


public static int digitSum (int n)
  { 
    int r = n%10;       //remainder, last digit of the number
    int num = n/10;     //the rest of the number without the last digit
    if(num == 0)
    {
      return n;
    } else {
      return digitSum (num) + r;
    }} 

      

0


source


This works for positive numbers.

public int sumDigits(int n) {
  int sum = 0;
  if(n == 0){
  return 0;
  }
  sum += n % 10; //add the sum
  n /= 10; //keep cutting
  return sum + sumDigits(n); //append sum to recursive call
}

      

0


source


    import java.util.Scanner;
public class Adder {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a number: ");
        System.out.println();
        int number = input.nextInt();
        System.out.println("The sum of the digits is " +adder(number));

    }
    public static int adder(int num){
        int length = String.valueOf(num).length();
        int first , last , sum;
        if (length==1){
            return num;
        }
        else
        {
            first = num /10;
            last = num % 10;
            sum = last + adder(first);
        }
        return sum;
    }
}

      

0


source


I see many solutions here, but none seem to be as simple as what follows. I've tested it countless times and this is not a problem:

public int sumDigits(int n) {
  if (n == 0){
    return 0;
  }
  else{
    return n%10 + sumDigits(n/10);
  }
}

      

0


source


#include <iostream>
int useRecursion(int x);
using namespace std;

int main(){
    int n;
    cout<<"enter an integer: ";
    cin>>n;
    cout<<useRecursion(n)<<endl;
    return 0;
}

int useRecursion(int x){
    if(x/10 == 0)
        return x;
    else
        return useRecursion(x/10) + useRecursion(x%10);
}

      

-1


source


I think this is the shortest option. However, the input thing is you too.

 public static int getSum(int input)  {  //example: input=246
       int sum=0;   
       if (input%10==input)  { //246%10=6;  
              return input%10; //2%10=2
       }

       return input%10+getSum((input-input%10)/10); //(246-6)/10=24; 24%10=4
 }

      

-1


source







All Articles