Number to words
PROBLEM :
Given number into words. For example, if “1234” is given as input, output should be “one thousand two hundred and thirty four”
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N.
Output:
Print the number into words (in small letter).
Constraints:
1 = T = 100
1 = N = 9999
Example:
Input:
6
101
717
9999
1000
1090
111
Output:
one hundred and one
seven hundred and seventeen
nine thousand nine hundred and ninety nine
one thousand
one thousand and ninety
one hundred and eleven
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
string PrintDigitWord11(int ) ;
string PrintDigitWord1(int ) ;
string PrintDigitWord2(int ) ;
string PrintDigitWord3(int ) ;
string PrintDigitWord4(int ) ;
int main()
{
int t,no,place,rem ;
cin>>t ;
while(t--)
{
cin>>no ;
string ans,str ;
str="" ;
ans="" ;
place=1 ;
while(no)
{
if(place==1)
{
rem=no%100 ;
str=PrintDigitWord11(rem) ;
if(str!="")
{
ans=str+ans ;
no=no/100 ;
place+=2 ;
continue ;
}
}
rem=no%10 ;
if(place==1)
str=PrintDigitWord1(rem),ans=str+ans ;
else if(place==2)
str=PrintDigitWord2(rem),ans=str+ans ;
else if(place==3)
{
if(ans!="")
ans="and "+ans;
str=PrintDigitWord3(rem) ;
ans=str+ans ;
}
else
str=PrintDigitWord4(rem),ans=str+ans ;
place++ ;
no=no/10 ;
}
cout<<ans<<endl ;
}
return 0;
}
string PrintDigitWord11(int no)
{
switch(no)
{
case 11 :return "eleven " ; break ;
case 12 :return "twelve " ; break ;
case 13 :return "thirteen " ; break ;
case 14 :return "fourteen " ; break ;
case 15 :return "fifteen " ; break ;
case 16 :return "sixteen " ; break ;
case 17 :return "seventeen " ; break ;
case 18 :return "eighteen " ; break ;
case 19 :return "nineteen " ; break ;
default : return "" ;
}
}
string PrintDigitWord1(int no)
{
switch(no)
{
case 1 :return "one " ; break ;
case 2 :return "two " ; break ;
case 3 :return "three " ; break ;
case 4 :return "four " ; break ;
case 5 :return "five " ; break ;
case 6 :return "six " ; break ;
case 7 :return "seven " ; break ;
case 8 :return "eight " ; break ;
case 9 :return "nine " ; break ;
default : return "" ;
}
}
string PrintDigitWord2(int no)
{
switch(no)
{
case 1 :return "ten " ; break ;
case 2 :return "twenty " ; break ;
case 3 :return "thirty " ; break ;
case 4 :return "forty " ; break ;
case 5 :return "fifty " ; break ;
case 6 :return "sixty " ; break ;
case 7 :return "seventy " ; break ;
case 8 :return "eighty " ; break ;
case 9 :return "ninety " ; break ;
default : return "" ;
}
}
string PrintDigitWord3(int no)
{
switch(no)
{
case 1 :return "one hundred " ; break ;
case 2 :return "two hundred " ; break ;
case 3 :return "three hundred " ; break ;
case 4 :return "four hundred " ; break ;
case 5 :return "five hundred " ; break ;
case 6 :return "six hundred " ; break ;
case 7 :return "seven hundred " ; break ;
case 8 :return "eight hundred " ; break ;
case 9 :return "nine hundred " ; break ;
default : return "" ;
}
}
string PrintDigitWord4(int no)
{
switch(no)
{
case 1 :return "one thousand " ; break ;
case 2 :return "two thousand " ; break ;
case 3 :return "three thousand " ; break ;
case 4 :return "four thousand " ; break ;
case 5 :return "five thousand " ; break ;
case 6 :return "six thousand " ; break ;
case 7 :return "seven thousand " ; break ;
case 8 :return "eight thousand " ; break ;
case 9 :return "nine thousand " ; break ;
default : return "" ;
}
}
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :(Similar But lesser code)
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
string PrintDigitWord11(int ) ;
string PrintDigitWord1(int ) ;
string PrintDigitWord2(int ) ;
int main()
{
int t,no,place,rem ;
cin>>t ;
while(t--)
{
cin>>no ;
string ans,str ;
str="" ;
ans="" ;
place=1 ;
while(no)
{
if(place==1)
{
rem=no%100 ;
str=PrintDigitWord11(rem) ;
if(str!="")
{
ans=str+ans ;
no=no/100 ;
place+=2 ;
continue ;
}
}
rem=no%10 ;
if(place==1)
str=PrintDigitWord1(rem),ans=str+ans ;
else if(place==2)
str=PrintDigitWord2(rem),ans=str+ans ;
else if(place==3)
{
if(ans!="")
ans="and "+ans;
str=PrintDigitWord1(rem);
if(ans!=""||rem!=0)
str=str+"hundred ";
ans=str+ans ;
}
else
{
str=PrintDigitWord1(rem);
if(ans!=""||rem!=0)
str=str+"thousand ";
ans=str+ans ;
}
place++ ;
no=no/10 ;
}
cout<<ans<<endl ;
}
return 0;
}
string PrintDigitWord1(int no)
{
switch(no)
{
case 1 :return "one " ; break ;
case 2 :return "two " ; break ;
case 3 :return "three " ; break ;
case 4 :return "four " ; break ;
case 5 :return "five " ; break ;
case 6 :return "six " ; break ;
case 7 :return "seven " ; break ;
case 8 :return "eight " ; break ;
case 9 :return "nine " ; break ;
default : return "" ;
}
}
string PrintDigitWord2(int no)
{
switch(no)
{
case 1 :return "ten " ; break ;
case 2 :return "twenty " ; break ;
case 3 :return "thirty " ; break ;
case 4 :return "forty " ; break ;
case 5 :return "fifty " ; break ;
case 6 :return "sixty " ; break ;
case 7 :return "seventy " ; break ;
case 8 :return "eighty " ; break ;
case 9 :return "ninety " ; break ;
default : return "" ;
}
}
string PrintDigitWord11(int no)
{
switch(no)
{
case 11 :return "eleven " ; break ;
case 12 :return "twelve " ; break ;
case 13 :return "thirteen " ; break ;
case 14 :return "fourteen " ; break ;
case 15 :return "fifteen " ; break ;
case 16 :return "sixteen " ; break ;
case 17 :return "seventeen " ; break ;
case 18 :return "eighteen " ; break ;
case 19 :return "nineteen " ; break ;
default : return "" ;
}
}
---------------------------------------------------------------------------------
Given number into words. For example, if “1234” is given as input, output should be “one thousand two hundred and thirty four”
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N.
Output:
Print the number into words (in small letter).
Constraints:
1 = T = 100
1 = N = 9999
Example:
Input:
6
101
717
9999
1000
1090
111
Output:
one hundred and one
seven hundred and seventeen
nine thousand nine hundred and ninety nine
one thousand
one thousand and ninety
one hundred and eleven
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
string PrintDigitWord11(int ) ;
string PrintDigitWord1(int ) ;
string PrintDigitWord2(int ) ;
string PrintDigitWord3(int ) ;
string PrintDigitWord4(int ) ;
int main()
{
int t,no,place,rem ;
cin>>t ;
while(t--)
{
cin>>no ;
string ans,str ;
str="" ;
ans="" ;
place=1 ;
while(no)
{
if(place==1)
{
rem=no%100 ;
str=PrintDigitWord11(rem) ;
if(str!="")
{
ans=str+ans ;
no=no/100 ;
place+=2 ;
continue ;
}
}
rem=no%10 ;
if(place==1)
str=PrintDigitWord1(rem),ans=str+ans ;
else if(place==2)
str=PrintDigitWord2(rem),ans=str+ans ;
else if(place==3)
{
if(ans!="")
ans="and "+ans;
str=PrintDigitWord3(rem) ;
ans=str+ans ;
}
else
str=PrintDigitWord4(rem),ans=str+ans ;
place++ ;
no=no/10 ;
}
cout<<ans<<endl ;
}
return 0;
}
string PrintDigitWord11(int no)
{
switch(no)
{
case 11 :return "eleven " ; break ;
case 12 :return "twelve " ; break ;
case 13 :return "thirteen " ; break ;
case 14 :return "fourteen " ; break ;
case 15 :return "fifteen " ; break ;
case 16 :return "sixteen " ; break ;
case 17 :return "seventeen " ; break ;
case 18 :return "eighteen " ; break ;
case 19 :return "nineteen " ; break ;
default : return "" ;
}
}
string PrintDigitWord1(int no)
{
switch(no)
{
case 1 :return "one " ; break ;
case 2 :return "two " ; break ;
case 3 :return "three " ; break ;
case 4 :return "four " ; break ;
case 5 :return "five " ; break ;
case 6 :return "six " ; break ;
case 7 :return "seven " ; break ;
case 8 :return "eight " ; break ;
case 9 :return "nine " ; break ;
default : return "" ;
}
}
string PrintDigitWord2(int no)
{
switch(no)
{
case 1 :return "ten " ; break ;
case 2 :return "twenty " ; break ;
case 3 :return "thirty " ; break ;
case 4 :return "forty " ; break ;
case 5 :return "fifty " ; break ;
case 6 :return "sixty " ; break ;
case 7 :return "seventy " ; break ;
case 8 :return "eighty " ; break ;
case 9 :return "ninety " ; break ;
default : return "" ;
}
}
string PrintDigitWord3(int no)
{
switch(no)
{
case 1 :return "one hundred " ; break ;
case 2 :return "two hundred " ; break ;
case 3 :return "three hundred " ; break ;
case 4 :return "four hundred " ; break ;
case 5 :return "five hundred " ; break ;
case 6 :return "six hundred " ; break ;
case 7 :return "seven hundred " ; break ;
case 8 :return "eight hundred " ; break ;
case 9 :return "nine hundred " ; break ;
default : return "" ;
}
}
string PrintDigitWord4(int no)
{
switch(no)
{
case 1 :return "one thousand " ; break ;
case 2 :return "two thousand " ; break ;
case 3 :return "three thousand " ; break ;
case 4 :return "four thousand " ; break ;
case 5 :return "five thousand " ; break ;
case 6 :return "six thousand " ; break ;
case 7 :return "seven thousand " ; break ;
case 8 :return "eight thousand " ; break ;
case 9 :return "nine thousand " ; break ;
default : return "" ;
}
}
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :(Similar But lesser code)
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
string PrintDigitWord11(int ) ;
string PrintDigitWord1(int ) ;
string PrintDigitWord2(int ) ;
int main()
{
int t,no,place,rem ;
cin>>t ;
while(t--)
{
cin>>no ;
string ans,str ;
str="" ;
ans="" ;
place=1 ;
while(no)
{
if(place==1)
{
rem=no%100 ;
str=PrintDigitWord11(rem) ;
if(str!="")
{
ans=str+ans ;
no=no/100 ;
place+=2 ;
continue ;
}
}
rem=no%10 ;
if(place==1)
str=PrintDigitWord1(rem),ans=str+ans ;
else if(place==2)
str=PrintDigitWord2(rem),ans=str+ans ;
else if(place==3)
{
if(ans!="")
ans="and "+ans;
str=PrintDigitWord1(rem);
if(ans!=""||rem!=0)
str=str+"hundred ";
ans=str+ans ;
}
else
{
str=PrintDigitWord1(rem);
if(ans!=""||rem!=0)
str=str+"thousand ";
ans=str+ans ;
}
place++ ;
no=no/10 ;
}
cout<<ans<<endl ;
}
return 0;
}
string PrintDigitWord1(int no)
{
switch(no)
{
case 1 :return "one " ; break ;
case 2 :return "two " ; break ;
case 3 :return "three " ; break ;
case 4 :return "four " ; break ;
case 5 :return "five " ; break ;
case 6 :return "six " ; break ;
case 7 :return "seven " ; break ;
case 8 :return "eight " ; break ;
case 9 :return "nine " ; break ;
default : return "" ;
}
}
string PrintDigitWord2(int no)
{
switch(no)
{
case 1 :return "ten " ; break ;
case 2 :return "twenty " ; break ;
case 3 :return "thirty " ; break ;
case 4 :return "forty " ; break ;
case 5 :return "fifty " ; break ;
case 6 :return "sixty " ; break ;
case 7 :return "seventy " ; break ;
case 8 :return "eighty " ; break ;
case 9 :return "ninety " ; break ;
default : return "" ;
}
}
string PrintDigitWord11(int no)
{
switch(no)
{
case 11 :return "eleven " ; break ;
case 12 :return "twelve " ; break ;
case 13 :return "thirteen " ; break ;
case 14 :return "fourteen " ; break ;
case 15 :return "fifteen " ; break ;
case 16 :return "sixteen " ; break ;
case 17 :return "seventeen " ; break ;
case 18 :return "eighteen " ; break ;
case 19 :return "nineteen " ; break ;
default : return "" ;
}
}
---------------------------------------------------------------------------------
Comments
Post a Comment