using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NumberWords
{
class Program
{
// This method converts a single-digit number (0-9)
// into the appropriate word ("zero"-"nine").
public static string oneDigit(int n)
{
switch (n)
{
case 0: return "zero";
case 1: return "one";
case 2: return "two";
case 3: return "three";
case 4: return "four";
case 5: return "five";
case 6: return "six";
case 7: return "seven";
case 8: return "eight";
case 9: return "nine";
default: return "?????";
}
}
// This method converts a two-digit number (10-99)
// into the appropriate word(s).
public static string twoDigit(int n)
{
if (n <= 19)
{
// For numbers from 10 to 19, we need a special
// set of rules to translate to words.
// *** add some code here similar to the code
// *** in the oneDigit method above
}
else
{
string s;
// For numbers from 20 to 99, the rules are simpler.
// First, determine if n is in the 20s, 30s, ... 90s:
switch (n / 10)
{
case 2: return "twenty";
case 3: return "thirthy";
case 4: return "forty";
case 5: return "fifth";
case 6: return "sixty";
case 7: return "seventy";
case 8: return "eighty";
case 9: return "ninety";
default: s = "?????"; break;
}
// At this point in the code, s should be equal to
// "twenty" or "thirty" or ... or "ninety".
// Next, determine what other word (if any) we need to add
// (for the ones digit).
{
}
// *** The line shown here is ROUGHLY the idea of what needs
// *** to happen at this point, but you will need to add
// *** more instructions to make this step work properly.
s = s + oneDigit(n % 10);
return s;
}
}
public static void numWords(int n)
{
// This method will eventually need to do more.
if (n < 10)
Console.WriteLine(oneDigit(n));
else if (n < 100)
Console.WriteLine(twoDigit(n));
else
Console.WriteLine("3-digit and bigger numbers don't work yet.");
}
static void Main(string[] args)
{
numWords(3);
numWords(17);
numWords(45);
numWords(70);
numWords(99);
numWords(516);
}
}
}
Mod Edit: Please use code tags when posting your code. Code tags are used like so =>
Thanks,
PsychoCoder

New Topic/Question
Reply




MultiQuote









|