69,382
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int compute(char s[],int n)
{
if (n>0)
{
return (s[n-1]-'0')+compute(s,n-1);
}
else
{
return 0;
}
}
int main()
{
int num,len;
char s[256];
printf("Please input a number:\n");
scanf("%d",&num);
sprintf(s,"%d",num);
len = strlen(s);
printf("Sum is %d\n",compute(s,len));
return 0;
}
测试
Please input a number:
123456789
Sum is 45
Process returned 0 (0x0) execution time : 2.465 s
Press any key to continue.