c语言练习29——输入一个不超过5位的数字,判断共几位数字,并逆序输出

#include
#include
/*题目:给出一个不超过5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。*/
int main(){
int a,a1,a2,a3,a4,a5;
while(1){
printf("请输入一个5位以内的数字:");
scanf("%d",&a);
if(a<=9)
printf("共1位数,逆序:%d\n",a);
else if(a<=99){
a1=a%10;
a2=a/10;
printf("共2位数,逆序:%d%d\n",a1,a2);}
else if(a<=999){
a1=a%10;
a2=a/10%10;
a3=a/100;
printf("共3位数,逆序:%d%d%d\n",a1,a2,a3);}
else if(a<=9999){
a1=a%10;
a2=a/10%10;
a3=a/100%10;
a4=a/1000;
printf("共4位数,逆序:%d%d%d%d\n",a1,a2,a3,a4);}
else if(a<=99999){
a1=a%10;
a2=a/10%10;
a3=a/100%10;
a4=a/1000%10;
a5=a/10000;
printf("共5位数,逆序:%d%d%d%d%d\n",a1,a2,a3,a4,a5);}
else 
printf("输入错误,请重新输入\n");
}
return 0;
}
   

 方法二

#include
#include

int main(){
int i,l;
char a[100];
while(1){
printf("请输入数字:");
for(i=0;i<100;i++){
scanf("%c",&a[i]);
if(a[i]=='\n')
break;}
l=i;
printf("共%d位数,倒序输出结果为:",l);
for(i=l-1;i>=0;i--)       //a[l]
printf("%c",a[i]);
printf("\n________________________________\n");
}
return 0;
}

 

转载于:https://www.cnblogs.com/gougouwang/p/11415600.html

你可能感兴趣的:(c/c++)