C++ Program to convert a number into characters


In this Program, You will learn How to Convert Number into Characters in C++.


123 => One Two Three

Example: How to Convert Number into Characters in C++.

#include<iostream>
using namespace std;

int main() {

   int n, rev = 0, r;

   cout << "Enter a number:";
   cin>>n;

   while (n > 0) {
       r = n % 10;
       rev = rev * 10 + r;
       n = n / 10;
   }
   n = rev;
   cout << "\n";

   while (n > 0) {
       r = n % 10;
       switch (r) {
           case 0:
               cout << "Zero ";
               break;

           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;
       }
       n = n / 10;
   }

   return 0;
}

Output:

Enter a number:2341

Two Three Four One