1

I need a Recursive algorithm to spell out any number from 1 to 2.1 billion. I already have the main part of the program sorted out but I am having trouble working Strings into the spelled out number.

For example 1,234,567 prints: "one thousand two hundred thirty four million five hundred sixty seven"

I understand why it is printing "thousand" before "million", I'm just having trouble finding the correct way to work in "hundred", "thousand", "million", and "billion" so that they are in the correct order. Thanks in advance.

static String[] numbers = { "zero", "one", "two", "three", "four", "five",
                            "six", "seven", "eight", "nine", "ten", "eleven",
                            "twelve", "thirteen", "fourteen", "fifteen",
                            "sixteen", "seventeen", "eighteen", "ninteen", "twenty"};

static String[] tens = {"twenty-", "thirty-", "fourty-", "fifty-",
                        "sixty-", "seventy-", "eighty-", "ninety-"};

//static String[] hundreds = {"hundred", "thousand", "million", "billion"};

private static String say(int n) {

    if (n <= 20) {
        return numbers[n];
    } 
    else if (n < 100) { // 99
        return tens[(n / 10) - 2] + say(n % 10); // TODO: fix seventy-zero
    } 
    else if (n < 1000) { // 999 max
        return say(n / 100) + " hundred " + say(n % 100);
    } 
    else if (n < 1000000) { // 999,999 max
        return say(n / 1000) + " thousand " + say(n % 1000);
    } 
    else if (n < 1000000000) { // 999,999,999 max
        return say(n / 1000) + " million " + say(n % 1000);
    }
    else if (n <= 2147483647) { // 2,147,483,647 max integer value
        return say(n / 1000) + " billion " + say(n % 1000);
    }
    else
        return "error";
}
0

2 Answers 2

1

you updated the hundred and thousand placing but forgot to update the million and billion placing.

else if (n < 1000000000) { // 999,999,999 max
    return say(n / 1000_000) + " million " + say(n % 1000_000);
}
else if (n <= 2147483647) { // 2,147,483,647 max integer value
    return say(n / 1000_000_000) + " billion " + say(n % 1000_000_000);
}
Sign up to request clarification or add additional context in comments.

2 Comments

as a side note, change the "zero" to "" to mute the printing of zeroes.
Thank You! That worked perfectly. With muting the zeroes, do you think it would be easier to just have one if statement dedicated to seeing if it's a zero? Then just have it as "" for all other ones?
1

First, you should remove all else words. You have return statement to terminate further execution on true condition.

Second, for example 12_345_678 will first satisfy n < 1_000_000_000condition. And will return say(12_000) + " million " + say(678); instead correct one will be say(12) + " million " + say (345_678);. To achieve it you need say(n / 1_000_000) + " million " + say(n % 1_000_000);

1 Comment

Thanks! That worked. I'll also get rid of those else statements, it makes sense why they're unnecessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.