Microsoft Interview Question
Software Engineer / Developers#include <stdio.h>
#include <string.h>
#include <math.h>
void numtostring(int ,int , char str[]);
main()
{
unsigned int num ;
char str[10];
char constr[1000]="";
int i;
printf("\n enter the number which do u want to convert in string : ");
scanf("%ud " , &num);
i = sprintf(str , "%d", num);
numtostring(num ,i , constr);
printf("\n string of given number : %d \n %s \n", num , constr);
}
void numtostring(int num , int len, char str[])
{
char *arr1[] = { "","One ","Two ","Three ","Four ","Five ","Six ","Seven ","Eight ","Nine "};
char* arr11[] ={"","Eleven ","Twelve ","Thirteen ","Fourteen ","Fifteen ","Sixteen ","Seventeen "," Eighteen ","Nineteen "};
char* arr10[] = {"","Ten ","Twenty ","Thirty ","Forty ","Fifty ","Sixty ","Seventy ","Eighty ","Ninety "};
char* arr100[] = {"","Hundred ","Thousand ","Lacs ","Crore "};
int i;
int tmp;
if(num == 0)
{
strcat(str , "Zero");
return ;
} if(len > 3 && len % 2 == 0)
{
len++;
}
do
{
if(len > 3)
{
tmp = (num/(int)pow(10,len-2));
if(tmp/10 == 1 && tmp%10 != 0)
{
strcat(str ,arr11[tmp%10] ) ;
}
else
{
strcat(str , arr10[tmp/10]);
strcat(str , arr1[tmp%10]);
}
if(tmp)
strcat(str ,arr100[len/2]);
num = num % (int)(pow(10,len-2));
len -= 2;
}
else
{
tmp = num / 100;
if(tmp != 0)
{
strcat(str , arr1[tmp]);
strcat(str ,arr100[len/2]);
}
tmp = num % 100 ;
if(tmp/10 == 1 && tmp%10 != 0)
{
strcat(str ,arr11[tmp%10] ) ;
}
else
{
strcat(str , arr10[tmp/10]);
strcat(str , arr1[tmp%10]);
}
len = 0;
}
}while(len > 0);
}
#include <stdio.h>
char *arr[][10] = {
{"One ","Two ","Three ","Four ","Five ","Six ","Seven ","Eight ","Nine "},
{"Ten","Eleven ","Twelve ","Thirteen ","Fourteen ","Fifteen ","Sixteen ","Seventeen "," Eighteen ","Nineteen "},
{"Twenty ","Thirty ","Forty ","Fifty ","Sixty ","Seventy ","Eighty ","Ninety "},
{"Thousand ","Lacs ","Crore ","Arab "}
};
void represent(int i){ //printing string equivalent of two digit num
int t1,t2;
t1=i/10;
t2=i%10;
if(i>0)
if(i<10)
printf("%s ",arr[0][t2-1]);
else if(i<20)
printf("%s ",arr[1][t2]);
else{
printf("%s ",arr[2][t1-2]);
if(t2>0)
printf("%s ",arr[0][t2-1]);
}
}
void trailthreedig(int num){ //printing string equi of trailing three digits
int t; //of input string
t=num/100;
num=num%100;
if(t != 0){
printf("%s",arr[0][t-1]);
printf("Hundred ");
}
represent(num);
}
void numtostring(int num) // a recursive function
{
int i=0;
static int count=0;
if(num==0)
return;
i=num%100;
count++;
numtostring(num/100);
represent(i);
printf("%s ",arr[3][count-1]);
count--;
}
main()
{
unsigned int num ;
int i;
printf("\n Enter the number string : ");
scanf("%ud " , &num);
printf("\n\nNumber: ");
if(num==0)
printf("Zero");
else{
numtostring(num/1000);
trailthreedig(num%1000);
}
printf("\n\n");
}
void Decompose(int i, int base)
{
if(!base)
return;
if( base == 10 && i < 20 && i > 10)
{
printf("Number is %d and base is %d\n", i, base);
return;
}
if(i/base)
{
if(i/base > 9)
{
Decompose(i/base, base/10);
}
printf("Number is %d and base is %d\n", i/base, base);
}
if(i%base)
{
Decompose(i%base, base/10);
}
}
class InWords
{
private:
map<int,string> m_map;
string HUNDRED ;
string THOUSAND ;
string getHundred(int no)
{
return m_map.find(no/100)->second + " " + HUNDRED + " and " + getTen(no%100);
}
string getTen(int no)
{
if (no%10 == 0)
return m_map.find(no)->second;
int temp = no;
while (temp%10 != 0)
{
temp = temp + 1;
}
// for 54 first will return fifty and second will return four
return m_map.find(temp - 10)->second + " " + m_map.find(no - (temp-10))->second;
}
string getThousand(int no)
{
return getWords(no/1000) + " " + THOUSAND + " " + getWords(no%1000);
}
public:
InWords()
{
HUNDRED = "hundred";
THOUSAND = "thousand";
m_map.insert(make_pair(0,""));
m_map.insert(make_pair(1,"one"));
m_map.insert(make_pair(2,"two"));
m_map.insert(make_pair(3,"three"));
m_map.insert(make_pair(4,"four"));
m_map.insert(make_pair(5,"five"));
m_map.insert(make_pair(6,"six"));
m_map.insert(make_pair(7,"seven"));
m_map.insert(make_pair(8,"eight"));
m_map.insert(make_pair(9,"nine"));
m_map.insert(make_pair(10,"ten"));
m_map.insert(make_pair(11,"eleven"));
<!-- add from 12 - 19 -->
m_map.insert(make_pair(20,"twenty"));
m_map.insert(make_pair(30,"thirty"));
m_map.insert(make_pair(40,"forty"));
m_map.insert(make_pair(50,"fifty"));
m_map.insert(make_pair(60,"sixty"));
m_map.insert(make_pair(70,"seventy"));
m_map.insert(make_pair(80,"eighty"));
m_map.insert(make_pair(90,"ninety"));
}
string getWords(int no)
{
if ( no >= 1000)
{
return getThousand(no);
} else if (no > 100)
{
return getHundred(no);
} else if (no > 10)
{
return getTen(no);
} else
{
return m_map.find(no)->second;
}
}
};
int main()
{
InWords w;
cout<<w.getWords(1004);
getch();
}
Any digit which has the maximum of 6 digits will be of seperated by thousand after first three digits so call first three digits and then append thousand and then call the last three digits
129212 - One hundred and twenty nine thousand and two hundread and twelve (American Standard).
void main(){
unsigned int num=129212;
String output1="";
String output2="";
int out1=num%1000;
int out2=num/1000;
output1=convert(out1);
if(out2!=0){
output2=convert(out2);
output1=output1+"thousand"+output2;
}
System.out.println(output1);
}
String convert(int num){
String word1[]={"zero","one",.....nine};
String word2[]={ten,eleven,......ninteen};
String word3[]={twenty,thirty,.....ninety};
String send;
int temp=num/100;
num=num%100;
if(temp!=0){
send=word1[temp]+"hundred";
temp=num/10;
}
else if(temp!=1 && temp!=0){
if(num%10!=0)
send=send+word[num%10];
else
send=send+word3[temp-2];
}
else if(temp==1)
send=send+word2[num%10];
else if (temp==0 && num%10!=0)
send=send+word1[num%10];
return send;
}
Vamsi, your idea is very good. Here is the working code :
#include <iostream>
#include <string>
using namespace std;
string convert(int num);
int main(int argc, char* [])
{
unsigned int num = 0;
string output1 = "";
string output2 = "";
cout << "Please input the number: " << endl;
cin >> num;
int out1 = num/1000;
int out2 = num%1000;
if(out1)
{
output1 = convert(out1) + " " + " thousand ";
cout << "Returned from convert first time" << endl;
}
if(out2)
{
output2 = convert(out2);
output1 = output1 + output2;
}
cout << "Output is: " << output1 << endl;
}
string convert(int num)
{
string word1[] = {"zero", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine", "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen"};
string word3[] = {"zero", "ten", "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"};
string send = "";
int temp = 0;
temp = num/100;
if(temp > 0)
{
send = word1[temp] + " " + "hundred " + "and";
}
num = num%100;
if(num > 0)
{
if(num > 10 && num < 20)
{
send = send + " " + word1[num];
}
else
{
temp = num/10;
if(temp >= 0)
send = send + " " + word3[temp];
num = num%10;
if(num > 0)
send = send + " " + word1[num];
}
}
return send;
}
Don't forget the test cases. If this question is for a test position, the second part of the question (test cases) is as important as the first (if not more so).
Some test cases for this problem:
-Acceptance case, an easy case just to see if it's working properly. Like 1234.
-Zero
-All numbers are the same digit
-All numbers are different digits
-A number with a lot of zeros like 500,000; 30,000; 2000, 100, 90.
-At least one number in the teens (11-19). This seems to be a tricky case. Make sure your other test data includes digits that end with a teen.
-Numbers with zeros in the middle: 500,008; 30,012; 908,301. Want to make sure zeros are handled correctly in the middle of the number. Try a zero in at least every position.
-Numbers less than 1000, to see if it outputs the word "thousand".
-Numbers with no "hundred" digit, so see if it doesn't out put the word "hundred".
They usually want you to spit out test cases like crazy until they ask you to stop. Be careful, though, don't just say foolish things in order to keep talking. Poor test cases are uncommon or bizarre inputs like a letter, another data type, etc. It would be great to handle these, but they are not that creative. Common user scenarios that can catch common pain points make better test cases. Boundary test cases are good but don't go crazy on these.
# include <stdio.h>
void printTheNumber(int);
void EnglishVersion1Digit(int);
void EnglishVersion2Digit(int);
static int count = 0;
main() {
int x = 10030089;
// scanf("Enter the number : %d",&x);
printTheNumber(x);
getch();
}
void printTheNumber(int x) {
int n; // Stores the next number to be processed
int nToPrint; // Stores the number to be printed
int tracker; // Keeps track of multiples
tracker = count;
if(x == 0) return;
if(count >= 2 || count == 0) {
n = x/100;
nToPrint = x - (n * 100);
}
else {
n = x/10;
nToPrint = x - (n * 10);
}
count++;
printTheNumber(n);
EnglishVersion1Digit(nToPrint);
if(nToPrint != 0) {
switch(tracker) {
case 1 : printf(" Hundred "); break;
case 2 : printf(" Thousand "); break;
case 3 : printf(" Lakhs "); break;
case 4 : printf(" Crore "); break;
}
}
}
void EnglishVersion1Digit(int nToPrint) {
switch(nToPrint) {
case 0 : printf(""); break;
case 1 : printf(" One "); break;
case 2 : printf(" Two "); break;
case 3 : printf(" Three "); break;
case 4 : printf(" Four "); break;
case 5 : printf(" Five "); break;
case 6 : printf(" Six "); break;
case 7 : printf(" Seven "); break;
case 8 : printf(" Eight "); break;
case 9 : printf(" Nine "); break;
default: EnglishVersion2Digit(nToPrint);
}
}
void EnglishVersion2Digit(int nToPrint) {
int tensPlace = nToPrint/10;
int unitsPlace = nToPrint - (tensPlace * 10);
switch(tensPlace) {
case 0 : printf(" "); break;
case 1 : EnglishVersion11Digit(unitsPlace); return;
case 2 : printf(" Twenty "); break;
case 3 : printf(" Thirty "); break;
case 4 : printf(" Fourty "); break;
case 5 : printf(" Fifty "); break;
case 6 : printf(" Sixty "); break;
case 7 : printf(" Seventy "); break;
case 8 : printf(" Eighty "); break;
case 9 : printf(" Ninty "); break;
}
if(unitsPlace != 0)
EnglishVersion1Digit(unitsPlace);
}
void EnglishVersion11Digit(int nToPrint) {
switch(nToPrint) {
case 0 : printf(" Ten "); break;
case 1 : printf(" Eleven "); break;
case 2 : printf(" Twelve "); break;
case 3 : printf(" Thirteen "); break;
case 4 : printf(" Fourteen "); break;
case 5 : printf(" Fifteen "); break;
case 6 : printf(" Sixteen "); break;
case 7 : printf(" Seventeen "); break;
case 8 : printf(" Eighteen "); break;
case 9 : printf(" Ninteen "); break;
}
}
#include <iostream>
#include <map>
#include <cmath>
#include <string>
using namespace std;
bool IntToStr(int num)
{
if(num<0||num>999999) return false;
map<int,string> intStr;
intStr[0]="";
intStr[1]="one";
intStr[2]="two";
intStr[3]="three";
intStr[4]="four";
intStr[5]="five";
intStr[6]="six";
intStr[7]="seven";
intStr[8]="eight";
intStr[9]="nine";
intStr[10]="ten";
intStr[11]="eleven";
intStr[12]="twelve";
intStr[13]="thirteen";
intStr[14]="fourteen";
intStr[15]="fifteen";
intStr[16]="sixteen";
intStr[17]="seventeen";
intStr[18]="eighteen";
intStr[19]="ninteen";
intStr[20]="twenty";
intStr[30]="thirty";
intStr[40]="forty";
intStr[50]="fifty";
intStr[60]="sixty";
intStr[70]="seventy";
intStr[80]="eighty";
intStr[90]="ninty";
if(num==0) cout<<0;
int len=0;
while(pow(10,++len)<=num);
string output("");
while(num>0)
{
switch(len)
{
case 1:
output+=intStr[num/pow(10,len-1)];
break;
case 2:
if(num>=10&&num<=20)
{
output+=intStr[num]+" ";
num=0;
}
else
output+=intStr[10*floor(num/pow(10,len-1))]+" ";
break;
case 3:
output+=intStr[num/pow(10,len-1)]+" hundred and ";
break;
case 4:
output+=intStr[num/pow(10,len-1)]+" thousand, ";
break;
case 5:
if(num>=10000&&num<=20000)
{
output+=intStr[num/1000]+" thousand, ";
--len;
}
else
output+=intStr[10*floor(num/pow(10,len-1))]+" ";
break;
case 6:
output+=intStr[num/pow(10,len-1)]+" hundred ";
break;
}
num=num%(int)pow(10,--len);
}
cout<<output<<endl;
return true;
}
void main()
{
IntToStr(113456);
}
this recursive method just following kunalrock 's way
===================
#include<iostream>
using namespace std;
print(int num)
{
switch(num)
{
case 0: break;
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;}
case 10: {cout<<"ten "; break;}
case 11: {cout<<"elevn "; break;}
case 12: {cout<<"twelve "; break;}
case 13: {cout<<"thirteen "; break;}
case 14: {cout<<"fourteen "; break;}
case 15: {cout<<"fifteen "; break;}
case 16: {cout<<"sixteen "; break;}
case 17: {cout<<"seventeen "; break;}
case 18: {cout<<"eighteen "; break;}
case 19: {cout<<"nineteen "; break;}
case 20: {cout<<"twenty "; break;}
case 30: {cout<<"thirty "; break;}
case 40: {cout<<"forty "; break;}
case 50: {cout<<"fifty "; break;}
case 60: {cout<<"sixty "; break;}
case 70: {cout<<"seventy "; break;}
case 80: {cout<<"eighty "; break;}
case 90: {cout<<"ninty "; break;}
case 1000: {cout<<"thousand "; break;}
case 1000000: {cout<<"million "; break;}
case 1000000000: {cout<<"billion "; break;}
}
}
parser(int subNum, int count)
{
if(subNum>99)
{
print(subNum/100);
subNum%=100;
cout<<"hundred ";
}
if(!subNum%10||(subNum>9&&subNum<21))
print(subNum);
else
{
print(10*(subNum/10));//
print(subNum%10);//
}
if(count>1)
print(count);
}
StrToInt(unsigned int num,int count)
{
if(!num||num>999999999) return;
int subNum=num%1000;
StrToInt(num/1000,count*1000);
parser(subNum,count);
}
main()
{
StrToInt(1234,1);
cout<<endl;
}
package com.careercup.book.four.one;
import java.util.Random;
public class Translate {
String one_digit[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
String two_digit1[] = { "ten", "eleven", "twelve", "thirteen", "fourth", "fifteen", "sixteen", "seventeen",
"eighteen", "nineteen" };
String two_digit[] = { "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety" };
String base0[] = { "ten", "hundred", "thousand" };
int get_thousand_digit(int n) {
return n / 1000;
}
int get_hundred_digit(int n) {
return (n - get_thousand_digit(n) * 1000) / 100;
}
int get_ten_digit(int n) {
return (n - get_thousand_digit(n) * 1000 - get_hundred_digit(n) * 100) / 10;
}
int get_single_digit(int n) {
return n % 10;
}
String convert_num_under_100(int n) {
String ret = "";
if (n < 10) {
return one_digit[n];
} else if (n < 20) {
return two_digit1[n - 10];
} else {
int t = get_ten_digit(n);
int s = get_single_digit(n);
ret += two_digit[t - 2];
if (s > 0) {
ret += " ";
ret += one_digit[s];
}
return ret;
}
}
String transition(int thousand, int hundred, int ten, int single, int pos) {
if (pos == 0) {
if (thousand == 0) {
return "";
} else {
if (hundred > 0 && (ten > 0 || single > 0)) {
return " ";
} else if (hundred > 0 && (!(ten > 0 || single > 0))) {
return " and ";
} else if (!(hundred > 0) && (ten > 0 || single > 0)) {
return " and ";
} else
return "";
}
} else if (pos == 1) {
if (thousand == 0 && hundred == 0) {
return "";
} else {
if (!(ten > 0 || single > 0)) {
return " ";
} else {
return " and ";
}
}
}
return "";
}
String translate_num(int n) {
String ret;
if (n < 0 || n > 99999) {
System.out.println("the number is out of the range [0..99999]");
ret = "Error";
return ret;
}
if (n == 0) {
ret = one_digit[0];
return ret;
}
int thousand = get_thousand_digit(n);
int hundred = get_hundred_digit(n);
int ten = get_ten_digit(n);
int single = get_single_digit(n);
ret = "";
if (thousand > 0) {
ret += convert_num_under_100(thousand);
ret += " ";
ret += base0[2];
ret += transition(thousand, hundred, ten, single, 0);
}
if (hundred > 0) {
ret += one_digit[hundred];
ret += " ";
ret += base0[1];
ret += transition(thousand, hundred, ten, single, 1);
}
if (ten > 0 || single > 0) {
ret += convert_num_under_100(ten * 10 + single);
}
return ret;
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
Random r = new Random();
int n = r.nextInt(99999);
System.out.println(n + " in english is " + (new Translate()).translate_num(n));
}
return;
}
}
Output
======
86045 in english is eighty six thousand and fourty five
20803 in english is twenty thousand eight hundred and three
69395 in english is sixty nine thousand three hundred and ninety five
93843 in english is ninety three thousand eight hundred and fourty three
55696 in english is fifty five thousand six hundred and ninety six
15704 in english is fifteen thousand seven hundred and four
54693 in english is fifty four thousand six hundred and ninety three
75689 in english is seventy five thousand six hundred and eighty nine
61250 in english is sixty one thousand two hundred and fifty
71631 in english is seventy one thousand six hundred and thirty one
Source: h t t p s : / / sites.google.com/site/spaceofjameschen/home/number-and-string/translate-number-into-english-phrases
public static String NumbToString(int numb){
HashMap<Integer, String> Map = new HashMap<Integer, String>();
Map.put(0,"zero");
Map.put(1,"one");
Map.put(2,"two");
Map.put(3,"three");
Map.put(4,"four");
Map.put(5,"five");
Map.put(6,"six");
Map.put(7,"seven");
Map.put(8,"eight");
Map.put(9,"nine");
Map.put(10,"ten");
Map.put(11,"eleven");
Map.put(12,"twelve");
Map.put(13,"thirteen");
Map.put(14,"fourteen");
Map.put(15,"fifteen");
Map.put(16,"sixteen");
Map.put(17,"seventeen");
Map.put(18,"eighteen");
Map.put(19,"nineteen");
Map.put(20,"twenty");
Map.put(30,"thirty");
Map.put(40,"forty");
Map.put(50,"fifty");
Map.put(60,"sixty");
Map.put(70,"seventy");
Map.put(80,"eighty");
Map.put(90,"ninety");
String result = "";
if(Map.containsKey(numb)){
return Map.get(numb);
}
else{
if(numb/1000>0)
result = result+NumbToString(numb/1000)+" thousand ";
if((numb%1000)/100>0)
result = result+NumbToString((numb%1000)/100)+" hundred and ";
if(((numb%1000)%100)/10 *10>0)
result = result+NumbToString(((numb%1000)%100)/10 *10)+" ";
if(((((numb%1000)%100)%10))/1>0)
result=result+NumbToString(((((numb%1000)%100)%10))/1);
}
return result;
}
I modified KAK's solution to account for cases where it does not work. The following works.
- ismailbhai September 11, 2007