If the numbers 1 to 5 are written out in words—one, two, three, four, five—then
ID: 3684012 • Letter: I
Question
If the numbers 1 to 5 are written out in words—one, two, three, four, five—then the number of letters used in total is 3 + 3 + 5 + 4 + 4 = 19 letters. If all the numbers from 1 to 1000 (one thousand), inclusive, were written out in words, how many letters would be used? Make a C++ program that:
a) include at least two functions where:
i. at least one function has to take in a parameter
ii. at least one function has to return a value
b) print out all numbers from 1 to 1000 as words
c) print out the total number of letters.
Do not count spaces or hypens. "And" is included, i.e. one hundred and sixteen has 20 letters. So my problem is formost formatting the program to print out ALL numbers from 1 to 1000 consequetively. My code is set up to prompt the user for an input which it then converts to english text; this made it much easier to figure out the conversion. Now I'm trying configure the program without a prompt, meaning it will run straight through and display the words consequetively. It must also display the total characters of the printed output (1 to 1000). I am using Code::Blocks with GCC Compiler.
#include <iostream>
using namespace std;
void expand(int);
int main()
{
int num;
cout<<"Enter a number : ";
cin>>num;
expand(num);
}
void expand(int value)
{
const char * const ones[20] = {"zero", "one", "two", "three","four","five","six","seven",
"eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen",
"eighteen","nineteen"};
const char * const tens[10] = {"", "ten", "twenty", "thirty","forty","fifty","sixty","seventy",
"eighty","ninety"};
if(value<0)
{
cout<<"minus ";
expand(-value);
}
else if(value>=1000)
{
expand(value/1000);
cout<<" thousand";
if(value % 1000)
{
if(value % 1000 < 100)
{
cout << " and";
}
cout << " " ;
expand(value % 1000);
}
}
else if(value >= 100)
{
expand(value / 100);
cout<<" hundred";
if(value % 100)
{
cout << " and ";
expand (value % 100);
}
}
else if(value >= 20)
{
cout << tens[value / 10];
if(value % 10)
{
cout << " ";
expand(value % 10);
}
}
else
{
cout<<ones[value];
}
return;
}
Explanation / Answer
#include <iostream>
#include<string>
using namespace std;
int expand(int,long int);
int main()
{
int num=expand(0,0);
cout<<" Count :"<<num;
}
int expand(int value,long int count)
{
string ones[20] = {"zero", "one", "two", "three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
string tens[10] = {"","ten", "twenty", "thirty","forty","fifty","sixty","seventy", "eighty","ninety"};
string hunderds[10] = {"", "one hunderd", "two hunderd", "three hunderd","four hunderd","five hunderd","six hunderd","seven hunderd","eight hunderd","nine hunderd"};
long int count1=count;
for(int i=1;i<=1000;i++)
{
value=i;
abc:
if(value==1000)
{
cout<<"one thousand";
count1=count1+11;
}
else if(value >= 100)
{
cout << hunderds[value / 100];
if(hunderds[value / 100]=="one hunderd") { count1=count1+10; }
else if(hunderds[value / 100]=="two hunderd") { count1=count1+10; }
else if(hunderds[value / 100]=="three hunderd") { count1=count1+12; }
else if(hunderds[value / 100]=="four hunderd") { count1=count1+11; }
else if(hunderds[value / 100]=="five hunderd") { count1=count1+11; }
else if(hunderds[value / 100]=="six hunderd") { count1=count1+10; }
else if(hunderds[value / 100]=="seven hunderd") { count1=count1+12; }
else if(hunderds[value / 100]=="eight hunderd") { count1=count1+12; }
else if(hunderds[value / 100]=="nine hunderd") { count1=count1+11; }
if(value%100>0)
{
cout << " and ";
count1=count1+3;
value=value%100;
goto abc;
}
else
{
cout<<" ";
}
}
else if(value >= 20)
{
cout << tens[value / 10];
if(tens[value/10]=="ten") { count1=count1+3; }
else if(tens[value/10]=="twenty") { count1=count1+6; }
else if(tens[value/10]=="thirty") { count1=count1+6; }
else if(tens[value/10]=="forty") { count1=count1+5; }
else if(tens[value/10]=="fifty") { count1=count1+5; }
else if(tens[value/10]=="sixty") { count1=count1+5; }
else if(tens[value/10]=="seventy") { count1=count1+7; }
else if(tens[value/10]=="eighty") { count1=count1+6; }
else if(tens[value/10]=="ninety") { count1=count1+6; }
if(value%10>0)
{
cout << " ";
value=value%10;
goto abc;
}
else
{
cout<<" ";
}
}
else
{
cout<<ones[value]<<endl;
if(ones[value]=="zero") { count1=count1+4; }
else if(ones[value]=="one") { count1=count1+3; }
else if(ones[value]=="two") { count1=count1+3; }
else if(ones[value]=="three") { count1=count1+5; }
else if(ones[value]=="four") { count1=count1+4; }
else if(ones[value]=="five") { count1=count1+4; }
else if(ones[value]=="six") { count1=count1+3; }
else if(ones[value]=="seven") { count1=count1+5; }
else if(ones[value]=="eight") { count1=count1+5; }
else if(ones[value]=="nine") { count1=count1+4; }
else if(ones[value]=="ten") { count1=count1+3; }
else if(ones[value]=="eleven") { count1=count1+6; }
else if(ones[value]=="twelve") { count1=count1+6; }
else if(ones[value]=="thirteen") { count1=count1+8; }
else if(ones[value]=="fourteen") { count1=count1+8; }
else if(ones[value]=="fifteen") { count1=count1+7; }
else if(ones[value]=="sixteen") { count1=count1+7; }
else if(ones[value]=="seventeen") { count1=count1+9; }
else if(ones[value]=="eighteen") { count1=count1+8; }
else if(ones[value]=="nineteen") { count1=count1+8; }
}
}
return count1;
}