none
converting numbertowords RRS feed

  • Question

  • Hi, 

    I need to write a code for 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)
            {
                
            }
            static public string NumberToWord(int number)
            {



                for (int i = -1; i <= 11; i++)

                {
                    Console.WriteLine("n = {0} => \"{1}\"", i, NumberToWord(i));
                    Console.ReadLine();

                }

                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();
                
                }

    Wednesday, July 25, 2018 6:10 PM

All replies

  • Have you considered a dictionary?

    var dict = new Dictionary<int, string>
    {
        [1] = "one",
        [2] = "two",
        [3] = "three"
    };
    
    var value = 91;
    if (dict.ContainsKey(value))
    {
        Console.WriteLine(dict[value]);
    }
    else
    {
        Console.WriteLine(value);
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, July 25, 2018 6:58 PM
    Moderator

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


    What *exactly* do you mean when you say "my code is not working"?

    In what way is it wrong? Be specific - don't make us guess.

    Also, when you post code please use the "Insert Code Block" feature of
    the forum editor. Look for the button on the menu bar.

    - Wayne

    Wednesday, July 25, 2018 8:41 PM

  • below is my code. 


            static void Main(string[] args)
            {
                
            }
            static public string NumberToWord(int number)
            {



                for (int i = -1; i <= 11; i++)

                {
                    Console.WriteLine("n = {0} => \"{1}\"", i, NumberToWord(i));
                    Console.ReadLine();

                }

                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();
                
                }

    Some issues with the code you posted:

    (1) Your Main function does nothing, so therefore the program will do nothing.

    (2) Why do you start the for loop with a value of -1?

    (3) Why do you end the for loop with a value of 11?

    (4) Your NumberToWord function is calling itself recursively. Did you
    really want to do that?

    (5) You should be checking to see if the number is valid in your Main 
    function, and then only call NumberToWord if it is valid.

    Note that since this is obviously an assignment, most of us will try hard
    NOT to give you a complete solution. Students are  supposed to do their own 
    assignments, not get others to do the coding for them. So we will try to
    walk you through the coding by giving suggestions that should steer you in
    a direction where you may discover or realize the solutions yourself.

    - Wayne

    Wednesday, July 25, 2018 8:59 PM

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

    below is my code. 


            static void Main(string[] args)
            {
                
            }
            static public string NumberToWord(int number)
            {



                for (int i = -1; i <= 11; i++)

                {
                    Console.WriteLine("n = {0} => \"{1}\"", i, NumberToWord(i));
                    Console.ReadLine();

                }

                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();
                
                }

    Try moving your test for loop into the Main method, and move the ReadLine()
    at the end of the NumberToWord method to the end of the Main method instead:

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

    - Wayne

    Wednesday, July 25, 2018 9:14 PM