C语言编程 递归方式实现打印一个整数的每一位

主要思想依旧是取位和调用数字的缩小
参照https://blog.51cto.com/14232799/2377396
源代码:

#include<stdio.h>
#include<stdlib.h>
void print(int n)
{
    if (n > 9)
    {
        print(n / 10);//数的缩小
    }
    printf("%-4d", n%10);//取位
}
int main()
{
    int number;
    printf("请输入要打印的数\n");
    scanf("%d", &number);
    print(number);
    system("pause");
    return 0;
}

猜你喜欢

转载自blog.51cto.com/14232799/2377407
今日推荐