Fastest method for adding/summing the individual digit components of a number

后端 未结 4 660
无人及你
无人及你 2021-02-03 10:53

I saw a question on a math forum a while back where a person was discussing adding up the digits in a number over and over again until a single digit is achieved. (i.e. \"362\"

4条回答
  •  Happy的楠姐
    2021-02-03 10:57

    int digit_sum(int n)
    Do
        if (n<10) return n;
        Exit do
        else
    
        n=n%10 + digit_sum(n/10);
    
    Loop 
    

提交回复
热议问题