Write a recursive function DigitSum(n), enter a non-negative integer, and return the sum of the numbers that make up it. For example, if you call DigitSum(1729), it should return 1 + 7 + 2 + 9, and its sum is 19.

c language

#include<stdio.h>
#include<stdlib.h>
int DigitSum(int x)
{
    if (x < 10)
        return x;
    else
        return (x % 10 + DigitSum(x / 10));
}

int main()
{
    int n = 1729;
    int ret = DigitSum(n);
    printf("%d\n", ret);
    system("pause");
    return 0;
}

More C language basic code practice address: http://blog.csdn.net/lxp_mujinhuakai,
everyone can discuss with me more.

Guess you like

Origin blog.csdn.net/lxp_mujinhuakai/article/details/54411703