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

前端 未结 12 1365
说谎
说谎 2020-12-06 14:34

So this is my code so far.

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


        
12条回答
  •  再見小時候
    2020-12-06 15:28

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

提交回复
热议问题