C語言 輸入一個不大於五位的數字,先判斷是幾位數字,然後將其數字順序輸出和逆序輸出
阿新 • • 發佈:2018-12-14
#include <stdio.h>
#include <math.h>
void main()
{
int function(int a);
int x,w,y,A[5];
printf("請輸入一個1至99999的整數\n");
scanf("%d",&x);
y=function(x);
w=y;
printf("您輸入的是一個%d位數\n",y);
//將數字按位放入陣列
for(int i=0;y!=0;i++)
{
A[i]=(int)x/(pow(10,y-1));
x=x%(int)(pow(10,y-1));
y--;}
printf("順序輸出為:\n");
for( int j=0;j<=w-1;j++)
{
printf("%4d",A[j]);
}
printf("\n");
printf("逆序輸出為:\n");
for(int k=w-1;k>=0;k--)
{
printf("%4d",A[k]);
}
printf("\n");
}
//計算該數字為幾位數
int function(int a)
{
int n=0;
while(a)
{
a/=10;
n++;
}
return n;
}