输入一个不多于5位的正整数,求它是几位数并且逆序打印出个位数字

#include
int main()
{
int i, j = 0, k, array[5], a, num, tmp, sum = 0;
printf("Please enter a small 5 digit integer:");
while(1)
{
if((scanf("%d",&num) != 1) || getchar() != '\n')
{
printf("Input error,please input again\n");
while(getchar() != '\n');
continue;
}
break;
}
tmp = num;
for(i = 0; i < 5; i++)  
{
a = tmp % 10;
sum++; //计算该整数的位数
array[j] = a; //用数组逆序存储该整数 
j++;
tmp = tmp / 10;
if(tmp == 0) //如果tmp为0,则直接结束循环体 
{
break;
}
}
printf("This integer is %d digits\n",sum);
for(k = 0; k < j; k++) //将数组存储的数据顺序打印出来 
{
printf("%d",array[k]);
}
return 0;
}

你可能感兴趣的:(C)