Programming Language: C#.net
Description: Write a program that convert the numbers into word sentence.
Ex. Input: 250 Output: Two-hundred and fifty
Solution: There are various solutions to solve this. But the hint is to write an algorithm which uses a string variable and concatenate it whenever the inputted number met certain requirements.
Things you need before starting:
- IDE(Visual Studio)
- Basic concept of OOP
- Basic knowledge of C# programming language
Today we’re going to play with numbers. If you wanted to convert numbers into sentence, if you're having problem with converting numbers into words this algorithm will solve it for you. This can also be useful in real-life scenario wherein you will create a program which will generate a cheque for the encoders like what I did within the office. So, let’s get started.
Just create a new console application within C#, and paste this method within your program:
//conversion of number to words constructor public static string NumberToWords(int number) { if (number == 0) return “zero”; if (number < 0) return “minus ” + NumberToWords(Math.Abs(number)); string words = “”; if ((number / 1000000) > 0) { words += NumberToWords(number / 1000000) + ” million “; number %= 1000000; } if ((number / 1000) > 0) { words += NumberToWords(number / 1000) + ” thousand “; number %= 1000; } if ((number / 100) > 0) { words += NumberToWords(number / 100) + ” hundred “; number %= 100; } if (number > 0) { if (words != “”) words += “and “; var unitsMap = new[] { “zero”, “one”, “two”, “three”, “four”, “five”, “six”, “seven”, “eight”, “nine”, “ten”, “eleven”, “twelve”, “thirteen”, “fourteen”, “fifteen”, “sixteen”, “seventeen”, “eighteen”, “nineteen” }; var tensMap = new[] { “zero”, “ten”, “twenty”, “thirty”, “forty”, “fifty”, “sixty”, “seventy”, “eighty”, “ninety” }; if (number < 20) words += unitsMap[number]; else { words += tensMap[number / 10]; if ((number % 10) > 0) words += “-” + unitsMap[number % 10]; } } return words; }
What about when the amount entered is a centavo?
This is a tricky one to solve. If you’re a programmer you will know what I mean. To solve this one, the key here is to scan the string inputted by the user and find the dot(.). Because, if there’s a dot in the value it will be considered a centavo value already. So I know you know what to do already but I’ll give you a code snippet on how I did it:
//paste this within Main Method string input = “”; bool isCentavo = false; string peso = “”; string centavo = “”; Console.Write(“Please input an amount: “); input = Console.ReadLine(); try //suppress errors due to outside bounds of array. You can fix it if you want { if (input.IndexOf(‘.’) != -1) { isCentavo = true; peso = input.Remove(input.IndexOf(‘.’)); for (int i = 0; i < 5; i++) { centavo += input[input.IndexOf('.') + i + 1]; //this code snippet will look for the centavo amount within the input. } } } catch (Exception) { }
And that’s it. You can now create a program which will convert number to words. Congratulations!
I’ve provided you a solution files so that you can see the source code. Download it here ->http://www.mediafire.com/download/whl2wk43bhvamcx/NumberToWords.7z
No comments:
Post a Comment