Friday, July 31, 2009

In C programming how to convert numbers into Indian number names?

If the input is a number (with any number of digits ,atleast six) how do u convert it to number name in the indian system?Ex: 47589 should yield an output like forty seven thousand five eighty nine.I need the simplest program possible..

In C programming how to convert numbers into Indian number names?
You should first get the number as a string using sprintf().





Once you have the string you should make a function that takes 2 parameters: A digit, the place of that digit in the number.





This function should return the right string of the number.


on your example: given 4 as the number %26amp; 5 as the place it should return the string 'forty' but if the place was 1 then it should return 'four'.





To help this function out you can make 2 other functions:


getNumberAsTens()


getNumberAsUnits()





Those return the different 2 strings of a digit


ex: Given 4 The first return 'forty' and the second return 'four'





Those 2 functions are for the first function to use.


Each of those 2 functions is basically one big switch case that takes the digit and return the string according to the value.





Then you should construct your final string by adding the words 'Thousands', 'hundreds' etc.. to make the final answer correct.





You will need to test all special cases to make sure you get them right like 100000 for example.
Reply:// Try something like this


//Brute force but simple:


//Create a table of strings


string s1[ ] =


{"zero";


"one";


...


"nine hundred ninety-nine"


};





Create anouther table of strings


string s2[ ] =


{"";


"thousand";


"million";


...


};





//Start with your number, n.





If( n == 0)


{


//output "zero".


// done


}











Otherwise,


int t = 0;





string OutputString;





while( 0 %26lt; n)


{


int r = n%1000;


if( r !=0)


{


OutputString =+ s1[r] + " " + s2[t] + OutputString;;


n = r;


t++;


}


}


No comments:

Post a Comment