dimanche 28 octobre 2018

Converting two number digit into words

I have written a program in c++ to convert number into words. The output for 0-19 is fine but it is giving wrong output for numbers between 20-99.

//This program converts numbers from 0-99 into words
#include<iostream>
using namespace std;
int main()
{
    int number,unit,ten;
    cout<<"Please enter any number between 0-99: ";
    cin >>number;
    ten=number/10;
    unit=number%10;
    if(number<0 | number>99)
        cout<<"Number is out of range";
    if(number>=11 & number <=19)
      {
        if(number==11) cout<<"eleven";
        if(number==12) cout<<"twelve";
        if(number==13) cout<<"thirteen";
        if(number==14) cout<<"fourteen";
        if(number==15) cout<<"fifteen";
        if(number==16) cout<<"sixteen";
        if(number==17) cout<<"seventeen";
        if(number==18) cout<<"eighteen";
        if(number==19) cout<<"ninteen";
      }
      else
      {
        if(unit==0) cout<<"zero";
        if(unit==1) cout<<"one"; 
        if(unit==2) cout<<"two";
        if(unit==3) cout<<"three";
        if(unit==4) cout<<"four";
        if(unit==5) cout<<"five";
        if(unit==6) cout<<"six";
        if(unit==7) cout<<"seven";
        if(unit==8) cout<<"eight";
        if(unit==9) cout<<"nine";
        if(ten==10) cout<<"Ten";
        if(ten==20) cout<<"twenty";
        if(ten==30) cout<<"thirty";
        if(ten==40) cout<<"fourty";
        if(ten==50) cout<<"fifty";
        if(ten==60) cout<<"sixty";
        if(ten==70) cout<<"seventy";
        if(ten==80) cout<<"eighty";
        if(ten==90) cout<<"ninty";
    }               
}   

Output:

Please enter any number between 0-99: 25
five

Aucun commentaire:

Enregistrer un commentaire