目录
(1)求解有几位数
(2)分别输出每一位数字
(3)按逆序输出各位数字
(1)求解有几位数
#include<stdio.h>unsigned int GetFigures(int n)
{unsigned int figures = 0; while(n!=0){n /= 10;figures++;}return figures;
}int main()
{printf("%d\n",GetFigures(12345));printf("%d\n",GetFigures(0));printf("%d\n",GetFigures(-12345));return 0;
}
(2)分别输出每一位数字
#include<stdio.h>
#include<math.h>
unsigned int GetFigures(int n)
{unsigned int figures = 0; while(n!=0){n /= 10;figures++;}return figures;
}
void PrintOrder(int n)
{unsigned char figures = GetFigures(n); /* 获取n的位数 */int power = (int)pow((float)10,(float)figures-1); /* 获取n最高位数字需要除模的数字 */int temp;if (0 == figures) /* n为0 */{printf("0\n");}if (n < 0) /* n小于0先打印“-”再处理数值 */{n = -n;printf("-");}while(power != 0){temp = n / power;n %= power;power /= 10;printf("%d",temp);}printf("\n");
}int main()
{PrintOrder(1234);return 0;
}
(3)按逆序输出各位数字
#include<stdio.h>
void PrintReverse(int n)
{int temp;if (0 == n) /* n为0 */{printf("0\n");}if (n < 0) /* n小于0先打印“-”再处理数值 */{n = -n;printf("-");}while(n != 0){temp = n % 10; n /= 10;printf("%d",temp);}printf("\n");
}int main()
{PrintReverse(1234);return 0;
}