# C语言实验——整数位

C语言实验——整数位

（1）求出它是几位数；

（2）分别输出每一位数字；

（3）按逆序输出各位数字。

``````123
``````

``````3
1 2 3
3 2 1
``````

``````#include<stdio.h>
int main(){
int i,n;
char a[5];
for(i=0;;i++){
a[i]=getchar();
if(a[i]=='\n'||a[i]==EOF){n=i;break;}
}
printf("%d\n",n);
n-=1;
for(i=0;i<n;i++)
printf("%c ",a[i]);
printf("%c\n",a[n]);
for(i=n;i>0;i--)
printf("%c ",a[i]);
printf("%c",a[0]);
return 0;
}
``````

``````#include<iostream>
using namespace std;
int main()
{
int x,a,b,c,d,e;
cin>>x;
if(x/10000!=0)
{
cout<<"5"<<endl;
a=x/10000;
b=x/1000%10;
c=x/100%10;
d=x/10%10;
e=x%10;
cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<endl;
cout<<e<<" "<<d<<" "<<c<<" "<<b<<" "<<a;
}
else if(x/10000==0&&x/1000!=0)
{
cout<<"4"<<endl;
a=x/1000;
b=x/100%10;
c=x/10%10;
d=x%10;
cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;
cout<<d<<" "<<c<<" "<<b<<" "<<a;
}
else if(x/10000==0&&x/1000==0&&x/100!=0)
{
cout<<"3"<<endl;
a=x/100;
b=x/10%10;
c=x%10;
cout<<a<<" "<<b<<" "<<c<<endl;
cout<<c<<" "<<b<<" "<<a;
}
else if(x/10000==0&&x/1000==0&&x/100==0&&x/10!=0)
{
cout<<"2"<<endl;
a=x/10;
b=x%10;
cout<<a<<" "<<b<<endl;
cout<<b<<" "<<a;
}
else if(x/10000==0&&x/1000==0&&x/100==0&&x/10==0&&x/1!=0)
{
cout<<"1"<<endl;
a=x%10;
cout<<a<<endl;
cout<<a;
}
return 0;
}``````