关于整数正序分解和整数逆序V2.0

 对整数进行正序和逆序分解 :

#include

int Count(int n)      //统计n是几位数字
{
	int temp = 0;
	do
	{
		n /= 10;
		temp ++;
	}while(n != 0);
	
	return temp;
}

void PrintOrder(int n)  //正序输出每个数字
{
	int c = Count(n);
	int power = 1;
	
	for(int i = 1;i <= c - 1 ;i ++)
	{
		power *= 10;
	}	
	
	do	
	{
		printf("%d ",n / power);		
		n %= power;		
		power /= 10;	
	}while(power>0);	
		printf("\n");
}

void PrintReverse(int n)//逆序输出每个数字
{
	int temp = 0;
	
	do
	{
		printf("%d ",n % 10);
		n /= 10;
	}while(n != 0);

	printf("\n");
}

int main()
{
	printf("%d\n",Count(0));
	PrintOrder(0);
	PrintReverse(0);
	printf("\n");
	
	printf("%d\n",Count(12345));
	PrintOrder(12345);
	PrintReverse(12345);
	printf("\n");
	
	printf("%d\n",Count(7000));
	PrintOrder(7000);
	PrintReverse(7000);
	printf("\n");
	
	return 0;
} 

运行如下: 

关于整数正序分解和整数逆序V2.0_第1张图片

 

你可能感兴趣的:(C语言,c)