递归小例子,数字正序、逆序输出

#include
//正序
void traverse(int n, int k);
int main(void)
{
int n;
scanf("%d", &n);
int lebal=1;
int work = n;
while (work/10 != 0)
{
lebal= lebal *10;
work /= 10;
}

traverse(n, lebal);

}

void traverse(int n, int lebal)
{
if(lebal == 1)
{
printf("%d “, n);
}
else
{
printf(”%d ", n/lebal);
traverse(n%lebal, lebal/10);
}

}

#include
//逆序输出
void reverse(int n);

int main(void)
{
int n;
scanf("%d", &n);
reverse(n);
return 0;
}

void reverse(int n)
{
if(n < 10)
{
printf("%d", n);
}

else
{
	printf("%d", n % 10);

	reverse(n / 10);
}

}

你可能感兴趣的:(C语言基础)