📜  C++示例 转换字符为数字

📅  最后修改于: 2020-10-16 07:37:47             🧑  作者: Mango

C++程序转换字符

在C++语言中,我们可以借助循环和切换大小写轻松地将数字转换为字符。在此程序中,我们从用户那里获取输入并对该数字进行迭代,直到它为0。在迭代过程中,我们将其除以10,然后将其余部分传递给switch大小写,以获得该数字的单词。

让我们看一下C++程序将数字转换成字符。

#include 
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