Given a positive integer with no more than 5 digits, the requirements are: 1. Find out how many digits it has; 2. Output each digit separately; 3. Output the digits in reverse order. For example: the original number 321 should output 123.
Given a positive integer with no more than 5 digits, the requirements are:
1. Find out how many digits it has;
2. Output each digit separately;
3. Output the digits in reverse order. For example: the original number 321 should be output 123.
code show as below:
#include<stdio.h>
int Digit(int n)//确定数字位数
{
int j,y,x=0,k=1;
j=n;//将输入的值用j保存,防止更改n的值
if(n==0)
{
printf("0是1位数");//如果没有该步骤,n=0进入循环得到位数为0位数
}
do//循环:去掉输入数字的最后一位数
{
j/=10;
x++;
}while(j);
return x;
}
void Reversedorder(int n)//逆序输出函数
{
printf("逆序输出:\n");
while(n!=0)
{
printf("%d\n",n%10);//通过求余输出最后一位数
n/=10;//去掉最后一位数
}
}
void order(int n)//顺序输出
{
printf("正序输出:\n");
while(n!=0)
{
printf("%d\n",n%10);
n/=10;
}
}
int main()
{
printf("%d\n",Digit(53247));
Reversedorder(53247);
order(53247);
return 0;
}