In: Computer Science
In 1-3 sentences, describe how you would "Change a number in to a word equivalent (132 -> one three two), Recursively" using C++
Description:
The above steps is clearly show in the code below. The above description will become more clear once you go through the code shown below.
CODE--
#include<bits/stdc++.h>
using namespace std;
string word_equivalent(string word,int num)
{
if(num==0)
{
return word;
}
switch(num%10)
{
case 0:
word="zero
"+word;
break;
case 1:
word="one
"+word;
break;
case 2:
word="two
"+word;
break;
case 3:
word="three
"+word;
break;
case 4:
word="four
"+word;
break;
case 5:
word="five
"+word;
break;
case 6:
word="six
"+word;
break;
case 7:
word="seven
"+word;
break;
case 8:
word="eight
"+word;
break;
case 9:
word="nine
"+word;
break;
}
return word_equivalent(word,num/10);
}
int main()
{
cout<<"132 = "<<
word_equivalent("",132)<<endl;
cout<<"12311 = "<<
word_equivalent("",12311)<<endl;
cout<<"12300 = "<<
word_equivalent("",12300)<<endl;
cout<<"100 = "<<
word_equivalent("",100)<<endl;
}
OUTPUT SCREENSHOT--
NOTE--
Please upvote if you like the effort.