[Functions]D. Liang 5.2 Summing the digits in an integer.c

Description

Write a function that computes the sum of the digits in an integer. Use the following function header:

int sumDigits(long n)

For example, sumDigits(234) returns (9)

//   Date:2020/3/28
//   Author:xiezhg5
int sumDigits(long n)
{
	if(n<0)
    n=-n;
    int i,sum=0;
    while(n/10!=0)
    {
    	i=n%10;
        sum=sum+i;
        n=n/10;
    }
    sum=sum+n;
    return sum;
}
发布了131 篇原创文章 · 获赞 94 · 访问量 2934

猜你喜欢

转载自blog.csdn.net/qq_45645641/article/details/105169607