translate numbers into words

translate numbers into words

Page 1 of 1

8 Replies - 2092 Views - Last Post: 16 November 2008 - 11:03 AM Rate Topic: -----

#1 heyloser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-November 08

translate numbers into words

Posted 16 November 2008 - 08:49 AM

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 => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: translate numbers into words

#2 Moonbat  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 34
  • View blog
  • Posts: 424
  • Joined: 30-June 08

Re: translate numbers into words

Posted 16 November 2008 - 08:56 AM

Why aren't you using break statements in between your cases? If you don't, it's going to keep checking if n equals to the other numbers, which it won't.

This post has been edited by Moonbat: 16 November 2008 - 08:59 AM

Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 6611
  • View blog
  • Posts: 23,941
  • Joined: 12-June 08

Re: translate numbers into words

Posted 16 November 2008 - 09:52 AM

The better thing to ask is - was there a question or a statement?
Was This Post Helpful? 0
  • +
  • -

#4 heyloser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-November 08

Re: translate numbers into words

Posted 16 November 2008 - 09:52 AM

View PostMoonbat, on 16 Nov, 2008 - 07:56 AM, said:

Why aren't you using break statements in between your cases? If you don't, it's going to keep checking if n equals to the other numbers, which it won't.



Well it works...i just need to figure out how to say like forty five...can you help at all?
Was This Post Helpful? 0
  • +
  • -

#5 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Re: translate numbers into words

Posted 16 November 2008 - 10:08 AM

You also need to use the code tags.
:code:
Was This Post Helpful? 0
  • +
  • -

#6 Moonbat  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 34
  • View blog
  • Posts: 424
  • Joined: 30-June 08

Re: translate numbers into words

Posted 16 November 2008 - 10:08 AM

Well, I would go about it like this. Let's say the number you get is 63.

First, find 63 modulus 10, that is to say, find the number that remains when you divide 63 by 10. You'll use the % (modulus operator) for this.

So you should get .3 as your answer. Assign this to a variable I'll call 'the_rem'.

Then take the original number, and divide it by 10. Subtract 'the_rem' from the answer you get. So 63 divided by 10 = 6.3 and then 6.3 - .3 = 6, and you assign the answer (6) to a variable we'll call 'tens_place'

So use your switch statement on 'tens_place' and since 'tens_place' = 6, you'll get sixty.

Then multiply 'the_rem' (which equals .3) by 10. You'll get 3 as your answer. Do a switch statement on this, and three will be your answer.

After that, you just put the two strings together when displaying them with Console.WriteLine().

NOTE: If 'the_rem' = 0, make sure you check for that in an if-statement so that 'the_rem' won't be shown. If your number was 60, you don't the user seeing sixty-zero as the answer.

EDIT: You'll be dealing with type double variables. So program accordingly.

This post has been edited by Moonbat: 16 November 2008 - 10:11 AM

Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1511
  • View blog
  • Posts: 5,916
  • Joined: 21-March 08

Re: translate numbers into words

Posted 16 November 2008 - 10:41 AM

View PostMoonbat, on 16 Nov, 2008 - 12:08 PM, said:

Well, I would go about it like this. Let's say the number you get is 63.

First, find 63 modulus 10, that is to say, find the number that remains when you divide 63 by 10. You'll use the % (modulus operator) for this.

So you should get .3 as your answer. Assign this to a variable I'll call 'the_rem'.

Then take the original number, and divide it by 10. Subtract 'the_rem' from the answer you get. So 63 divided by 10 = 6.3 and then 6.3 - .3 = 6, and you assign the answer (6) to a variable we'll call 'tens_place'

So use your switch statement on 'tens_place' and since 'tens_place' = 6, you'll get sixty.

Then multiply 'the_rem' (which equals .3) by 10. You'll get 3 as your answer. Do a switch statement on this, and three will be your answer.

After that, you just put the two strings together when displaying them with Console.WriteLine().

NOTE: If 'the_rem' = 0, make sure you check for that in an if-statement so that 'the_rem' won't be shown. If your number was 60, you don't the user seeing sixty-zero as the answer.

EDIT: You'll be dealing with type double variables. So program accordingly.


63 / 10 = 6
63 % 10 = 3

there is no need to subtract, and no need to use doubles. using integers will actually work out better.

This post has been edited by eclipsed4utoo: 16 November 2008 - 10:42 AM

Was This Post Helpful? 0
  • +
  • -

#8 Moonbat  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 34
  • View blog
  • Posts: 424
  • Joined: 30-June 08

Re: translate numbers into words

Posted 16 November 2008 - 10:50 AM

View Posteclipsed4utoo, on 16 Nov, 2008 - 09:41 AM, said:

63 / 10 = 6
63 % 10 = 3

there is no need to subtract, and no need to use doubles. using integers will actually work out better.

Oh, I was thinking literally (like 63 / 10 = 6.3 so I thought 63 % 10 would give .3).
Was This Post Helpful? 0
  • +
  • -

#9 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1511
  • View blog
  • Posts: 5,916
  • Joined: 21-March 08

Re: translate numbers into words

Posted 16 November 2008 - 11:03 AM

here is how I did it:

Main method
static void Main(string[] args)
{
    Console.Write("Enter a number between 0 and 100: ");
    int number = int.Parse(Console.ReadLine());

    if (number < 10)
    {
        Console.WriteLine(OneDigit(number));
    }
    else if (number < 100)
    {
        Console.WriteLine(TwoDigit(number));
    }
    else
    {
        Console.WriteLine("not allowed");
    }

    Console.ReadLine();
}



One Digit method
static string OneDigit(int oneDigit)
{
    string oneDigitName = string.Empty;

    switch (oneDigit)
    {
        case 1:
            oneDigitName = "one";
            break;
        case 2:
            oneDigitName = "two";
            break;
        case 3:
            oneDigitName = "three";
            break;
        case 4:
            oneDigitName = "four";
            break;
        case 5:
            oneDigitName = "five";
            break;
        case 6:
            oneDigitName = "six";
            break;
        case 7:
            oneDigitName = "seven";
            break;
        case 8:
            oneDigitName = "eight";
            break;
        case 9:
            oneDigitName = "nine";
            break;
        case 0:
            oneDigitName = "zero";
            break;
    }

    return oneDigitName;
}



Two Digit method
static string TwoDigit(int twoDigit)
{
    string twoDigitName = string.Empty;

    if (twoDigit < 20)
    {
        switch (twoDigit)
        {
            case 10:
                twoDigitName = "ten";
                break;
            case 11:
                twoDigitName = "eleven";
                break;
            case 12:
                twoDigitName = "twelve";
                break;
            case 13:
                twoDigitName = "thirteen";
                break;
            case 14:
                twoDigitName = "fourteen";
                break;
            case 15:
                twoDigitName = "fifteen";
                break;
            case 16:
                twoDigitName = "sixteen";
                break;
            case 17:
                twoDigitName = "seventeen";
                break;
            case 18:
                twoDigitName = "eighteen";
                break;
            case 19:
                twoDigitName = "nineteen";
                break;
        }
    }
    else
    {
        switch (twoDigit / 10)
        {
            case 2:
                if (!CheckForZeroRemainder(twoDigit))
                    twoDigitName = "twenty-" + OneDigit(twoDigit % 10);
                else
                    twoDigitName = "twenty";
                break;
            case 3:
                if (!CheckForZeroRemainder(twoDigit))
                    twoDigitName = "thirty-" + OneDigit(twoDigit % 10);
                else
                    twoDigitName = "thirty";
                break;
            case 4:
                if (!CheckForZeroRemainder(twoDigit))
                    twoDigitName = "fourty-" + OneDigit(twoDigit % 10);
                else
                    twoDigitName = "fourty";
                break;
            case 5:
                if (!CheckForZeroRemainder(twoDigit))
                    twoDigitName = "fifty-" + OneDigit(twoDigit % 10);
                else
                    twoDigitName = "fifty";
                break;
            case 6:
                if (!CheckForZeroRemainder(twoDigit))
                    twoDigitName = "sixty-" + OneDigit(twoDigit % 10);
                else
                    twoDigitName = "sixty";
                break;
            case 7:
                if (!CheckForZeroRemainder(twoDigit))
                    twoDigitName = "seventy-" + OneDigit(twoDigit % 10);
                else
                    twoDigitName = "seventy";
                break;
            case 8:
                if (!CheckForZeroRemainder(twoDigit))
                    twoDigitName = "eighty-" + OneDigit(twoDigit % 10);
                else
                    twoDigitName = "eighty";
                break;
            case 9:
                if (!CheckForZeroRemainder(twoDigit))
                    twoDigitName = "ninety-" + OneDigit(twoDigit % 10);
                else
                    twoDigitName = "ninety";
                break;
            case 0:
                twoDigitName = OneDigit(twoDigit % 10);
                break;
        }
    }

    return twoDigitName;
}



and a method to check if the remainder is zero(20,30,40, etc.)
static bool CheckForZeroRemainder(int num)
{
    if (num % 10 == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1