【c语言】给一个不多于五位的正整数: 1.求出它是几位数 2.分别输出每一位数字 3.逆序输出各位数字

//给一个不多于五位的正整数:
//1.求出它是几位数
//2.分别输出每一位数字
//3.按逆序输出各位数字

#include
#include

//统计n是几个数字
//算法:每次丢弃个位数字(n/=10)
int Getfigures(int n)//123456
{
	int count = 0;
	if (n == 0)
	{
		return 1;
	}
	while (n != 0)
	{
		count++;
		n /= 10;//丢弃个位
	}
	return count;
}

//逆序输出(123->3 2 1)
//算法:得到个位数字,丢弃个位数字
void PrintReverse(int n) //void:没有返回值
{
	do
	{
		printf("%d ", n % 10); //得到个位数字
		n /= 10; //丢弃个位数字
	} while (n != 0);
	printf("\n");
}

//顺序输出 (1234->1 2 3 4)
void PrintOrder(int n)
{
	int c = Getfigures(n);
	int power = pow(10.0, c - 1);
	do
	{
		printf("%d ", n / power);
		n %= power;
		power /= 10;
	} while (n != 0);
	printf("\n");
}

int main()
{
	printf("%d\n", Getfigures(123456));
	PrintReverse(123);
	PrintOrder(1234);
	return 0;
}

你可能感兴趣的:(【c语言】给一个不多于五位的正整数: 1.求出它是几位数 2.分别输出每一位数字 3.逆序输出各位数字)