给出一个不多于3位的正整数n要求:1求出它是几位数;2分别打印出每一位数字数字之间加一个空格;3按逆序打印出各位数字数字之间加一个空格。C语言程序为:
#include <stdio.h>
int main()
{
int n, digit, count = 0;
scanf("%d", &n);
int temp = n; // 用temp保存n,因为后面需要改变n的值
while (temp)
{
digit = temp % 10; // 取出最低位数字
count++; // 位数加一
printf("%d ", digit); // 打印每一位数字
temp /= 10; // 去掉最低位数字
}
printf("\n");
while (n)
{
digit = n % 10; // 取出最低位数字
printf("%d ", digit); // 按逆序打印每一位数字
n /= 10; // 去掉最低位数字
}
printf("\n%d位数", count);
return 0;
}
原文地址: https://www.cveoy.top/t/topic/Nfi 著作权归作者所有。请勿转载和采集!