Posted on 2006-03-27 08:51
我爱C 阅读(1320)
评论(3) 编辑 收藏 引用 所属分类:
疑难作业习题解答
5.7 给一个不多于5位的正整数,要求:①求他是几位数;②分别打印出每一位数字;③按逆序打印出各位数字。
参考程序:
main()
{ long int num;
int indiv,ten,hundred,thousand,ten_thousand,palce;/*分别代表个位,十位,百位,千位,万位和位数*/
printf("please input a integer(0-99999):");
scanf("%ld",&num);
if(num>9999)
place=5;
else if(num>999)
place=4;
else if(num>99)
palce=3;
else if(num>9)
place=2;
else
place =1;
printf("place=%d\n",place);
ten_thousand=num/10000;
thousand=(num-ten_thousand*10000)/1000;
hundred=(num-ten_thousand*10000-thousand*1000)/100;
ten=(num-ten_thousand*10000-thousand*1000-hundred*100)/10;
indiv=num-ten_thousand*10000-thousand*1000-hundred*100-ten*10;
switch(place)
{case 5:printf("%d,%d,%d,%d,%d\n",ten_thousand,thousand,hundred,ten,indiv);
printf("%d,%d,%d,%d,%d\n",indiv,ten,hundred,thousand,ten_thousand);
break;
case 4:printf("%d,%d,%d,%d\n",thousand,hundred,ten,indiv);
printf("%d,%d,%d,%d\n",indiv,ten,hundred,thousand);
break;
case 3:printf("%d,%d,%d\n",hundred,ten,indiv);
printf("%d,%d,%d\n",indiv,ten,hundred);
break;
case 2:printf("%d,%d\n",ten,indiv);
printf("%d,%d\n",indiv,ten);
break;
case 1:printf("%d\n",indiv);
printf("%d\n",indiv);
}
}