Microsoft Interview Question
Software Engineer / Developerswrite a function which will print under hundred i.e 0-99 then pass the number two digit then one digit then two digit then one digit into a stack .. when this is done pop them and pass them to printUpToHundred() function which will do the printing stuff and in the main you print lakh,then thousand then hundres take care of the case 1lakh and 2lakhs also
0 is also a special case.
1-19 are special case
tenth place- define all(20,30,40...90)
hundredth place- define all(100,200...)
thousandth place- define all(1000,2000,...)
if(num==0)write zero
else...
{
use the above data stored to display the num.
special case... 'dont display zeroes'.
e.g. 1024- one thousand, twenty, four
e.g. 2318- two thousand, three hundred eighteen.
}
#include <stdio.h>
#include <string.h>
void main()
{
char *unit_words[] = {"zero", "one","two","three","four","five","six","seven","eight","nine"};
char *teen_words[] = {"ten", "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
char *ten_words[] = {"error", "error","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
char hundred[] = " hundred";
char and[] = " and ";
char value_str[50] = "";
int value = 0; /* Integer to be converted */
int digits[] = {0,0,0}; /* Stores digits of value entered */
int i = 0;
printf("Enter an integer less than 1000: ");
scanf("%d",&value);
if(value>=1000)
value =999;
else if(value<1)
value = 1;
while(value>0)
{
digits[i++] = value%10;
value /= 10;
}
if(digits[2] > 0)
{
strcat(strcat(value_str,unit_words[digits[2]]), hundred);
if(digits[1] >0 || digits[0] > 0)
strcat(value_str, and);
}
if(digits[1] > 0)
{
if(digits[1] == 1)
strcat(value_str,teen_words[digits[0]]);
else
{
strcat(value_str,ten_words[digits[1]]);
if(digits[0] > 0)
strcat(strcat(value_str, " "), unit_words[digits[0]]);
}
}
else
if(digits[0] > 0)
strcat(value_str, unit_words[digits[0]]);
printf("\n%s\n", value_str);
}
http://www.java2s.com/Code/C/Data-Type/Convertanintegertowords.htm
- Karthik February 02, 2009