Number to words program

104 Views Asked by At

I need to write this kind of program:

Write a subroutine to take a numeric value from 1 to 9 as an argument and return the English name (such as one, two, or nine). If the value is out of range, return the original number as the name instead. Test it with some input data; you will have to write some sort of Main program to call the subroutine, for example, using a for loop.

The program should look like this:

n = -1 => "-1" 

n = 0 => "0"

n = 1 => "one"

I kept trying but for some reason my code is not working.

Below is my code:

    static void Main(string[] args)
    {  
         for (int i = -1; i <= 11; i++)
          {
            Console.WriteLine("n = {0} => \"{1}\"", i, NumberToWord(i));
            Console.ReadLine();
           }
    }

    static public string NumberToWord(int number)
    {
        string[] words =
        { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"

            ;
        if ((number >= 1) && (number <= 9))
        {
            return words[number - 1];
        }

        else
        {
            return number.ToString();
        }

        Console.ReadLine();

        }
1

There are 1 best solutions below

2
Sujash Agarwal On
string[] words =
      { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

change the array declaration as above.