C语言教程-将数字转换为字符的C程序
![](https://wx.javatiku.cn/j/j301-t.jpg)
数字转换为字符:在C语言中,我们可以通过循环和switch case很容易地将数字转换为字符。在这个程序中,我们从用户那里获取输入,然后迭代这个数字,直到它变为0。在迭代过程中,我们将其除以10,余数通过switch case传递,以获取数字对应的单词。
让我们看一下将数字转换为字符的C程序。
#include <stdio.h>
#include <stdlib.h>
int main() {
long int n, sum = 0, r;
system("cls");
printf("Enter the number: ");
scanf("%ld", &n);
while (n > 0) {
r = n % 10;
sum = sum * 10 + r;
n = n / 10;
}
n = sum;
while (n > 0) {
r = n % 10;
switch (r) {
case 1:
printf("one ");
break;
case 2:
printf("two ");
break;
case 3:
printf("three ");
break;
case 4:
printf("four ");
break;
case 5:
printf("five ");
break;
case 6:
printf("six ");
break;
case 7:
printf("seven ");
break;
case 8:
printf("eight ");
break;
case 9:
printf("nine ");
break;
case 0:
printf("zero ");
break;
default:
printf("tttt");
break;
}
n = n / 10;
}
return 0;
}
输出:
Enter the number: 4321
four three two one