1. 程式人生 > >給出一個不多於五位的正整數,要求輸出它的位數,每一位數字及逆序

給出一個不多於五位的正整數,要求輸出它的位數,每一位數字及逆序

#include<stdio.h>
#include<math.h>
int main()
{
	int a,b,c,d,e,f;
	int count=0;
	printf("input a:\n");
	scanf("%d",&a);
	if(a>9999)
	{
		count=5;
	}
	else if(a>999)
	{
		count=4;
	}
	else if(a>99)
	{
		count=3;
	}
	else if(a>9)
	{
		count=2;
	}
	else
	{
		count=1;
	}
	printf("count=%d\n",count);
	printf("它的每一位數字為:");
	b=a/10000;
	c=(int)(a-b*10000)/1000;
	d=(int)(a-b*10000-c*1000)/100;
	e=(int)(a-b*10000-c*1000-d*100)/10;
	f=(int)(a-b*10000-c*1000-d*100-e*10);
	switch(count)
	{
		case 5:printf("%d%d%d%d%d",b,c,d,e,f);
				printf("\n反序數字為:\n");
				printf("%d%d%d%d%d",f,e,d,c,b);
				break;
		case 4:printf("%d%d%d%d",c,d,e,f);
				printf("\n反序數字為:\n");
				printf("%d%d%d%d",f,e,d,c);
				break;
		case 3:printf("%d%d%d",d,e,f);
				printf("\n反序數字為:\n");
				printf("%d%d%d",f,e,d);
				break;
		case 2:printf("%d%d",e,f);
				printf("\n反序數字為:\n");
				printf("%d%d",f,e);
				break;
		case 1:printf("%d",f);
				printf("\n反序數字為:\n");
				printf("%d",f);
				break;
		}
	printf("\n");
	return 0;
}