C++ 将数字转换成字符
在C++语言中,我们可以通过循环和switch语句轻松将数字转换成字符。在这个程序中,我们接收用户的输入,并在数字变为0之前进行迭代。在迭代过程中,我们将数字除以10,并将余数传递给switch语句,以得到数字对应的单词。
下面是C++程序将数字转换成字符的示例。
#include <iostream>
using namespace std;
int main()
{
long int n,sum=0,r;
cout<<"Enter the Number= ";
cin>>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:
cout<<"one ";
break;
case 2:
cout<<"two ";
break;
case 3:
cout<<"three ";
break;
case 4:
cout<<"four ";
break;
case 5:
cout<<"five ";
break;
case 6:
cout<<"six ";
break;
case 7:
cout<<"seven ";
break;
case 8:
cout<<"eight ";
break;
case 9:
cout<<"nine ";
break;
case 0:
cout<<"zero ";
break;
default:
cout<<"tttt ";
break;
}
n=n/10;
}
}
输出:
Enter the Number= 74254
seven four two five four