C语言程序:计算数字位数、输出每一位数字并逆序输出
#include <stdio.h>\n\nint main() {\n int n;\n printf("请输入一个不超过5位的整数:");\n scanf("%d", &n);\n\n // 求n的位数\n int count = 0;\n int temp = n;\n while (temp != 0) {\n temp /= 10;\n count++;\n }\n printf("n的位数为:%d\n", count);\n\n // 分别输出每一位数字\n printf("每一位数字依次为:");\n temp = n;\n int digit;\n while (temp != 0) {\n digit = temp % 10;\n printf("%d ", digit);\n temp /= 10;\n }\n printf("\n");\n\n // 按逆序输出\n printf("逆序输出为:");\n while (n != 0) {\n digit = n % 10;\n printf("%d", digit);\n n /= 10;\n }\n printf("\n");\n\n return 0;\n}
原文地址: https://www.cveoy.top/t/topic/l9bU 著作权归作者所有。请勿转载和采集!