c语言 函数递归的简单应用

       利用函数递归来时现将一个sh数的每一位拆出来然后求和,即是:例如一个shu数  1888;它的每一位sh是 1  8  8   8,而每一位的每一位的和最终是  25,而接下来jian建立用函数的递归来sh实现这个算法。

#include<stdio.h>
int add(int souce,int ant)
{
	if (souce > 9)
	{
		ant = ant + souce % 10;
		return add(souce / 10, ant);
	}
	else
		return ant+souce;
}
int main()
{
	int a = 1888;
	int b = 0;
	printf("%d", add(a,b));
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/f_shell_x/article/details/81321054