C++ program to enter a number and print it into words

C++ program to enter a number and print it into words

#include<iostream.h>
#include<conio.h>

void once(int a)
{
switch(a)
{
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;
}
}

int tens(int a,int b)
{
int flag=0;
switch(a)
{
case 1:
flag=1;
switch(b)
{
case 0: cout<<“Ten”;
break;
case 1: cout<<“Eleven”;
break;
case 2: cout<<“Twelve”;
break;
case 3: cout<<“Thirteen”;
break;
case 4: cout<<“Fourteen”;
break;
case 5: cout<<“Fifteen”;
break;
case 6: cout<<“Sixteen”;
break;
case 7: cout<<“Seventeen”;
break;
case 8: cout<<“Eighteen”;
break;
case 9: cout<<“Nineteen”;
break;
}
break;
case 2: cout<<“Twenty”;
break;
case 3: cout<<“Thirty”;
break;
case 4: cout<<“Fourty”;
break;
case 5: cout<<“Fifty”;
break;
case 6: cout<<“Sixty”;
break;
case 7: cout<<“Seventy”;
break;
case 8: cout<<“Eighty”;
break;
case 9: cout<<“Ninety”;
break;
}
return(flag);
}

void hundred(int a)
{
switch(a)
{
case 1: cout<<“One Hundred”;
break;
case 2: cout<<“Two Hundred”;
break;
case 3: cout<<“Three Hundred”;
break;
case 4: cout<<“Four Hundred”;
break;
case 5: cout<<“Five Hundred”;
break;
case 6: cout<<“Six Hundred”;
break;
case 7: cout<<“Seven Hundred”;
break;
case 8: cout<<“Eight Hundred”;
break;
case 9: cout<<“Nine Hundred”;
break;
}
}

void main()
{
clrscr();
int n,a[3],i=0,flag=0;
cout<<“Enter any number(max 3 digits):”;
cin>>n;

while(n!=0)
{
a[i++]=n%10;
n=n/10;
}

for(i=i-1;i>=0;–i)
{
if(i==0&&flag==0)
once(a[0]);
if(i==1)
flag=tens(a[1],a[0]);
if(i==2)
hundred(a[2]);
cout<<” “;
}
getch();
}

6 thoughts on “C++ program to enter a number and print it into words

  1. bhavani luniya

    #include
    #include
    #include

    class CH_W
    {
    int value;
    public:
    CH_W();
    void Word(int);
    void Tens(int,int);
    };

    CH_W::CH_W()
    {
    cout<<"nEnter The No.(Max 3 digit)t";
    cin>>value;
    if(value<10)
    {
    Word(value);
    }
    else if(value<100)
    {
    Tens(abs(value/10),value%10);
    if(value/10!=1)
    Word(value%10);
    }
    else
    {
    Word(abs(value/100));
    cout<<" Hundred";
    Tens((abs(value/10))%10,(value%100)%10);
    if(abs((value%100)/10)!=1)
    Word((value%100)%10);
    }
    }

    void CH_W::Word(int value)
    {
    switch(value)
    {
    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;
    }
    }

    void CH_W::Tens(int value1,int value2)
    {
    switch (value1)
    {
    case 1:
    {
    switch(value2)
    {
    case 1: cout<<" Elaven";
    break;
    case 2: cout<<" Twel";
    break;
    case 3: cout<<" Thirteen";
    break;
    case 4: cout<<" Fourteen";
    break;
    case 5: cout<<" Fifteen";
    break;
    case 6: cout<<" Sixteen";
    break;
    case 7: cout<<" Seventeen";
    break;
    case 8: cout<<" Eighteen";
    break;
    case 9: cout<<" Ninteen";
    break;
    }
    break;
    }
    case 2: cout<<" Twenty";
    break;
    case 3: cout<<" Thirty";
    break;
    case 4: cout<<" Fourty";
    break;
    case 5: cout<<" Fifty";
    break;
    case 6: cout<<" Sixty";
    break;
    case 7: cout<<" Seventy";
    break;
    case 8: cout<<" Eighty";
    break;
    case 9: cout<<" Ninty";
    break;
    }
    }

    void main()
    {
    clrscr();
    CH_W o;
    getch();
    }

    Reply
    1. Narayanan K

      Neeraj Mishra your 100% correct….. My suggestion also same. Why you want to work under somebody, work as freedom . Get jobs from freelancer sites and develop by your own.

      Reply
  2. Muhsin Aydın

    and this is from me 🙂

    # include
    # include

    main()
    {
    int xyz,x,y,z;

    printf("3 basamakli Sayiyi giriniz:");
    scanf("%d",&xyz);
    z= xyz%10;
    y= ((xyz – z)/10)%10;
    x= (((xyz-(y*10))-z)/100);

    if(x!=0)
    {
    switch(x)
    {

    case 1:
    printf("");
    break;
    case 2:
    printf("iki");
    break;
    case 3:
    printf("uc");
    break;
    case 4:
    printf("dort");
    break;
    case 5:
    printf("bes");
    break;
    case 6:
    printf("alti");
    break;
    case 7:
    printf("yedi");
    break;
    case 8:
    printf:("sekiz");
    break;
    case 9:
    printf("dokuz");
    break;

    }
    printf(" yuz ");
    switch(y)
    {
    case 1:
    printf("on ");
    break;
    case 2:
    printf("yirmi ");
    break;
    case 3:
    printf("otuz ");
    break;
    case 4:
    printf("kirk ");
    break;
    case 5:
    printf("elli ");
    break;
    case 6:
    printf("altmis ");
    break;
    case 7:
    printf("yetmis ");
    break;
    case 8:
    printf("seksen ");
    break;
    case 9:
    printf("doksan ");
    break;
    }
    switch(z)
    {
    case 0:
    printf("");
    break;
    case 1:
    printf("bir");
    break;
    case 2:
    printf("iki");
    break;
    case 3:
    printf("uc");
    break;
    case 4:
    printf("dort");
    break;
    case 5:
    printf("bes");
    break;
    case 6:
    printf("alti");
    break;
    case 7:
    printf("yedi");
    break;
    case 8:
    printf("sekiz");
    break;
    case 9:
    printf("dokuz");
    break;

    }
    }
    else {

    printf("Sayi Uc Basamakli Degildir");
    }
    getchar();

    return 0;

    }

    Reply
  3. Todd Goodfellow

    You will develop a C++ program to spell out numbers given in a file. Assume that you have text file appears as

    15
    -568
    3432
    9001
    16045

    Input file has one number (short) (minus or plus or zero) at each line. A number can have at most 5 characters, such as 12356, or -1245. For the above file, your program should generate

    fifteen
    minus five hundred sixty eight
    three thousand four hundred sixty two
    nine thousand one
    sixteen thousand forty five


    I should use function short, which is a number read from file, and returns a string, which is spelling out of number, i.e., the prototype seems like

    string Spell (short a);

    can some one please help?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *