Write a C++ program to convert a number into words up to one Billion (logic explained step by step)

Here is the C++ code to convert a number into words up to one Billion . It takes input from user for a number and displays the number in words. You all should have in mind how it works exactly. Here is the algorithm.

  Steps
User side
1: User asked to enter a number (the number should be in the range of one billion)
2: Screen displays the result on screen.
Developer side
1: Declare variables that are going to be used in program .
2: Take a number as input from user
3: Pass the number to the function first.
4: This function display the corresponding word if the number is twenty or less than twenty and multiples of ten.
5: If the number is not multiple of ten or not less than twenty, then if the number is within the range of 100 pass it to the function 'twodigits()'. It will break the number in digits and send both the digits to the function 'first'. The 'first' function will return a string that will be concatenated to the string from the function 'twodigit' and the control goes back to the main function.
6: If the number is greater than one hundred and within the range of one thousand then pass it to the function 'threedigit' 
7: This function will break the number into digits. First digit is passed to the function 'first()' and a string "Hundred and" will be concatenated to the returned string. And other two digits will be passed to the function 'twodigits()'. It will repeat the step number 5 and the returned string will concatenated with the above string and control goes back to the main function .
8: If the number is greater than one thousand and within the range of 10000 then pass it to the function 'fourdigit' . It will split first digit that will be passed to the function 'first' that will return a string that will be concatenated with the string "Thousand". Other three digits will be passed to the function 'threedigit' and step 7 will be repeated. The returned string will be concatenated with the above string.
9: If the number is greater than 10000 and within the range of 100000 then pass it to the function 'fivedigit()'. It will split first digit that will be passed to the function 'first' that will return a string that will be concatenated with the string "Thousand". Other three digits will be passed to the function 'threedigit' and step 7 will be repeated. The returned string will be concatenated with the above string.
10: If the number is greater than 100000 and within the range of 1000000 pass it to the function 'sixdigit()'. It will split first digit that will be passed to the function 'first' that will return a string that will be concatenated with the string "Lack". Other digits will be passed to the function 'fivedigit' and step 9 will be repeated. The returned string will be concatenated with the above string.
11:  If the number is greater than 100000 and within the range of 1000000 pass it to the function 'seven digit()'. It will split first digit that will be passed to the function 'first' that will return a string that will be concatenated with the string "Lack". Other digits will be passed to the function 'fivedigit' and step 9 will be repeated. The returned string will be concatenated with the above string.
12: If the number is greater than 1000000 and within the range of 10000000 pass it to the function 'eightdigit()'. It will split first digit that will be passed to the function 'first' that will return a string that will be concatenated with the string "Crore". Other digits will be passed to the function 'sevendigits' and step 9 will be repeated. The returned string will be concatenated with the above string.
13:  If the number is greater than 10000000 and within the range of 100000000 pass it to the function 'eightdigit()' and so on, do it up to ten digits .
14: Program ends here.

Code:

#include <iostream>
#include <string>
using namespace std;

string first(unsigned long unsigned long int);
string twodigit(unsigned long unsigned long int);
string threedigit(unsigned long unsigned long int);
string fourdigit(unsigned long unsigned long int);
string fivedigit(unsigned long unsigned long int);
string sixdigit(unsigned long unsigned long int);
string sevendigit(unsigned long unsigned long int);
string eightdigit(unsigned long unsigned long int);
string ninedigit(unsigned long unsigned long int);
string tendigit(unsigned long unsigned long int);
string str;
unsigned long int main()
{      string wrd;
       unsigned long unsigned long int n;
       cout<<"Enter the number between 1 billion";
       cin>>n;
       cout<<"You entered "<<n<<endl;
      
       wrd=first(n);
       cout<<wrd<<endl;
      

       system("pause");
       return 0;
}
//one digits, tens, etc....
string first(unsigned long unsigned long int n)
{      if(n<=20)
       {     
       switch(n)
                     {
                           case 0:
                           str="zero";
                           break;
                           case 1:
                           str="one ";
                           break;case 2:
                           str="two ";
                           break;case 3:
                           str="three ";
                           break;case 4:
                           str="four ";
                           break;case 5:
                           str="five ";
                           break;case 6:
                           str="six ";
                           break;case 7:
                           str="seven ";
                           break;case 8:
                           str="eight ";
                           break;case 9:
                            str="nine ";
                           break;
                           case 10:
                           str="Ten ";
                           break;
                           case 11:
                           str="Eleven ";
                           break;case 12:
                           str="Twelve ";
                           break;case 13:
                           str="Thirteen ";
                           break;case 14:
                           str="Fourteen ";
                           break;case 15:
                           str="Fifteen ";
                            break;case 16:
                           str="Sixteen ";
                           break;case 17:
                           str="Seventeen ";
                           break;case 18:
                           str="Eighteen ";
                           break;case 19:
                           str="Ninteen ";
                           break;case 20:
                           str="Tewenty ";
                           break;
                           default:
                          
                                  break;
                           }
                     }
       else if(n%10==0 && n<=90)
       {
                     switch(n)
                           {
                           case 30:
                           str="Thirty ";
                           break;case 40:
                           str="Fourty ";
                           break;case 50:
                           str="Fifty ";
                           break;case 60:
                           str="Sixty ";
                           break;case 70:
                           str="Seventy ";
                           break;case 80:
                           str="Eighty ";
                           break;case 90:
                           str="ninty ";
                           break;
                           default:
                          
                                  break;
                     }
              }
      
       else
              if(n<=99){
                     str=twodigit(n);}
                     else if(n>=100&&n<=999)
                                  {str=threedigit(n);}
                           else if(n>=1000 &&n<=9999)
                                  {str=fourdigit(n);}
                                  else if(n>=10000&&n<=99999)
                                  {str=fivedigit(n);}
                                         else if(n>=100000&&n<=999999)
                                                {str=sixdigit(n);}
                                                else if(n>=1000000&&n<=9999999)
                                                       {str=sevendigit(n);}
                                                else if(n>=10000000&&n<=99999999)
                                                       {str=eightdigit(n);}
                                                else if(n>=100000000&&n<=999999999)
                                                       {str=ninedigit(n);}
                                                else if(n>=1000000000&&n<=9999999999)
                                                       {str=tendigit(n);}

      
       return str;
}
//two digits
string twodigit(unsigned long unsigned long int n)
{      string rtn;
       unsigned long int firstdigit,seconddigit;
       firstdigit=n/10;
       firstdigit=firstdigit*10;

       seconddigit=n%10;
       rtn=first(firstdigit);
       rtn=rtn + first(seconddigit);
       return rtn;
}

//three digits
string threedigit(unsigned long unsigned long int n)
{      string rtn;
       unsigned long int firstdigit,seconddigit,third;
       firstdigit=n/100;
       seconddigit=n%100;
       rtn=first(firstdigit);

       rtn=rtn+" Hundered and ";
       rtn=rtn + first(seconddigit);
       return rtn;
}
//four digits
string fourdigit(unsigned long unsigned long int n)
{      string rtn;
       unsigned long int firstdigit,seconddigit;
       firstdigit=n/1000;
       seconddigit=n%1000;
       rtn=first(firstdigit);
       rtn=rtn+" Thousand ";
       rtn=rtn + threedigit(seconddigit);
       return rtn;
}
//five digits
string fivedigit(unsigned long unsigned long int n)
{      string rtn;
       unsigned long int firstdigit,seconddigit;
       firstdigit=n/1000;
       seconddigit=n%1000;
       rtn=first(firstdigit);
       rtn=rtn+" Thousand ";
       rtn=rtn + threedigit(seconddigit);
       return rtn;
}
//six digits
string sixdigit(unsigned long unsigned long int n)
{      string rtn;
       unsigned long int firstdigit,seconddigit;
       firstdigit=n/100000;
       seconddigit=n%100000;
       rtn=first(firstdigit);
       rtn=rtn+" Lack ";
       rtn=rtn + fivedigit(seconddigit);
       return rtn;
}
//seven digits
string sevendigit(unsigned long unsigned long int n)
{      string rtn;
       unsigned long int firstdigit,seconddigit;
       firstdigit=n/100000;
       seconddigit=n%100000;
       rtn=first(firstdigit);
       rtn=rtn+" Lack ";
       rtn=rtn + fivedigit(seconddigit);
       return rtn;
}

//eight digits
string eightdigit(unsigned long unsigned long int n)
{      string rtn;
       unsigned long int firstdigit,seconddigit;
       firstdigit=n/10000000;
       seconddigit=n%10000000;
       rtn=first(firstdigit);
       rtn=rtn+" Crore ";
       rtn=rtn + sevendigit(seconddigit);
       return rtn;
}
//nine digits
string ninedigit(unsigned long unsigned long int n)
{      string rtn;
       unsigned long int firstdigit,seconddigit;
       firstdigit=n/10000000;
       seconddigit=n%10000000;
       rtn=first(firstdigit);
       rtn=rtn+" Crores ";
       rtn=rtn + sevendigit(seconddigit);
       return rtn;
}
//Ten digits
string tendigit(unsigned long unsigned long int n)
{      string rtn;
       unsigned long int firstdigit,seconddigit;
       firstdigit=n/1000000000;
       seconddigit=n%1000000000;
       rtn=first(firstdigit);
       rtn=rtn+" Billion ";
       rtn=rtn + ninedigit(seconddigit);
       return rtn;
}


Output:



Comments