Epsilon Interview Question
InternsCountry: United States
Interview Type: In-Person
Write a function which will convert the number bellow 1K to the string description, as suggested by the question, then write a function which separate the the original number into x billion, y million, and Z K and the rest R, call the first function to covert X, Y, Z, and K into the description, put a unit name (billion, million, thousand) at the end of each description, and combine these short phrases into final results.
My solution is not in module so it may happen You face problem to understand but still it is good solution.
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
using namespace std;
int main()
{
enum numbers {thirty=21,forty,fifty,sixty,seventy,eighty,ninety,hundred,thousand};
char number[][10]={"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen",
"fourteen","fifteen","sixteen","seventeen","eighteen","ninteen","twenty","thirty","forty","fifty","sixty",
"seventy","eighty","ninety","hundred","thousand"};
char buf[10];
int n,k,placeValue,value;
cout<<"Enter number"<<endl;
cin>>n;
if(n<=20)
{
cout<<number[n]<<endl;
}
else
{
k=sprintf(buf,"%d",n);
placeValue=pow(10,k-1);
while(n>=20)
{
value=n/placeValue;
n=n%placeValue;
if(value>0)
{
if(k>2)
cout<<number[value]<<" ";
switch(k)
{
case 4:
cout<<number[thousand]<<" ";
break;
case 3:
cout<<number[hundred]<<" ";
break;
case 2:
cout<<number[18+value]<<" ";
break;
}
}
placeValue=placeValue/10;
--k;
}
if(n>0)
{
cout<<number[n];
}
cout<<endl;
}
return 0;
}
Here you can see the o/p, just you have to enter a number.
h+t+t+p://ideone.com/do6kR5#view_edit_box
class NumberWordsConv{
static String one[] = {"", "one ", "two ", "three ", "four ","five ", "six ", "seven ", "eight ","nine ", "ten ", "eleven ", "twelve ","thirteen ", "fourteen ", "fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen "};
static String ten[] = {"", "", "twenty ", "thirty ", "forty ","fifty ", "sixty ", "seventy ", "eighty ", "ninety " };
static String numToWords(int n, String s) {
String str = "";
if (n > 19) {
str += ten[n / 10] + one[n % 10];
} else {
str += one[n];
}
if (n != 0) {
str += s;
}
return str;
}
static String convertToWords(long n) {
String out = "";
out += numToWords((int) (n / 10000000), "crore ");
out += numToWords((int) ((n / 100000) % 100), "lakh ");
out += numToWords((int) ((n / 1000) % 100), "thousand ");
out += numToWords((int) ((n / 100) % 10), "hundred ");
if (n > 100 && n % 100 > 0) {
out += "and ";
}
out += numToWords((int) (n % 100), "");
return out;
}
public static void main(String[] args) {
long n = 438237764;
System.out.printf(convertToWords(n));
}
}
class NumberWordsConv{
static String one[] = {"", "one ", "two ", "three ", "four ","five ", "six ", "seven ", "eight ","nine ", "ten ", "eleven ", "twelve ","thirteen ", "fourteen ", "fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen "};
static String ten[] = {"", "", "twenty ", "thirty ", "forty ","fifty ", "sixty ", "seventy ", "eighty ", "ninety " };
static String numToWords(int n, String s) {
String str = "";
if (n > 19) {
str += ten[n / 10] + one[n % 10];
} else {
str += one[n];
}
if (n != 0) {
str += s;
}
return str;
}
static String convertToWords(long n) {
String out = "";
out += numToWords((int) (n / 10000000), "crore ");
out += numToWords((int) ((n / 100000) % 100), "lakh ");
out += numToWords((int) ((n / 1000) % 100), "thousand ");
out += numToWords((int) ((n / 100) % 10), "hundred ");
if (n > 100 && n % 100 > 0) {
out += "and ";
}
out += numToWords((int) (n % 100), "");
return out;
}
public static void main(String[] args) {
long n = 438237764;
System.out.printf(convertToWords(n));
}
}
convert number to corresponding digits and put in array.
- helios March 15, 2013write switch case statement from 0 to 9 and put in for loop .