5.7 給一個不多于5位的正整數(shù),要求:①求他是幾位數(shù);②分別打印出每一位數(shù)字;③按逆序打印出各位數(shù)字。
參考程序:
main()
{?? long int num;
???? int? indiv,ten,hundred,thousand,ten_thousand,palce;/*分別代表個位,十位,百位,千位,萬位和位數(shù)*/
??? 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);
??? }
}