Pages

Subscribe:

About Template

Random Posts

C++ Program That Converts A Number Into Words


Name: Number Into Words Converter
By: Ammar Ahmed
Description: This program displays the Arabic Number in words or descriptive form like 5222 = Five Thousand Two Hundred Twenty Two

As you can see in the screenshot below, I entered the number 99999,then, it displayed the numbers in thousands, hundreds, tens and ones. Also, it displayed their corresponding words, Ninety Nine Thousand Nine Hundred Ninety Nine. You must input number ranges from 0-99999.

//Below are the Codes of the Program:

#include<iostream.h>
char *ones[10]={" One"," Two"," Three"," Four"," Five"," Six"," Seven"," Eight"," Nine"};
char *seconds[10]={" Eleven"," Twelve"," Thirteen"," Fourteen"," Fifteen"," Sixteen"," Seventeen"," Eighteen"," Nineteen"};
char *tens[10]={" Ten"," Twenty"," Thirty"," Forty"," Fifty"," Sixty"," Seventy"," Eighty"," Ninety"};
void main()
{
    int number,res,terminate=1;
    long int a,b,c,d,e;
    while(terminate!=0)
    {
    cout << "Enter the number between 0-99999 : ";
    cin >> number;
    while(number<0 || number >100000)
    {
        cout << endl << "Error please enter the numbers between 0-99999 : ";
        cin >> number;
    }
    a = number - (number%10000);
    b = (number%10000) - (number%1000);
    c = (number%1000) - (number%100);
    d = (number%100) - (number%10);
    e = number%10;
    cout << "Numbers : " << a << " " << b << " " << c << " " << d << " " << e << endl;
    if(number==0)
    {
        cout << endl << " Zero";
    }
    if((a+b)==10000)
    {
        cout << tens[0];
    }
    if((a+b) > 10000 && (a+b) < 20000)
    {
        res = ((a+b)/1000)%10 - 1;
        cout << seconds[res] << " " << " Thousand";
    }
    if(a>10000)
    {
        res = (a/10000)-1;
        cout << tens[res];
    }
    if((a+b)%10000==0 && (a+b) > 99)
    {
        cout << " Thousand";
    }
    if((a+b)%10000!=0 && ((a+b)<10000 || (a+b)>20000))
    {
        res = b/1000 -1;
        cout << ones[res] << " " << " Thousand";
    }
    if((a+b+c)%1000 != 0)
    {
        res = c/100 -1;
        cout << ones[res] << " " << " Hundred";
    }
    if(d == 0 && e != 0)
    {
        res = e-1;
        cout << ones[res];
    }
    if(d == 10 && e != 0)
    {
        res = e-1;
        cout << seconds[res];
    }
    if((d+e)%10 == 0 && (d+e) != 0)
    {
        res = d/10 - 1;
        cout << tens[res];
    }
    if((d+e) > 20 && (d+e)%10 != 0)
    {
        res = d/10 - 1;
        cout << tens[res] << " " << ones[e-1];
    }
    if(number==100000)
    {
        cout << " (Above the range!!!)  ";
    }
    cout << endl << "For running again press 1 or 0 for Exit : ";
    cin >> terminate;
    cout << endl;
    }
}