Numbers to Words


Collapse Content

Given a number from 1 to 4 (inclusive), return a word representation of the number. For example, given 2, return two. If the number is greater than 4, return the phrase too large. If the number is less than 1, return the phrase too small.

Input/Output Format
Boilerplate code is provided for Java. For other languages, read in the numbers from the standard input in the following format.

  • The input starts with N, the number of cases.
  • N lines follow, which each contains 1 integer.

Print out the correct phrase for each case on its own line.

Click on "View Raw I/O" below to view sample input and output.

Challenge

'Convert' each Number to a Word, as described above.

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Comments

  • An interesting thing I discovered in this task is that you can't use return statements within a switch-block otherwise the compiler will say the break statements are unreachable. I had to assign

    cont...
  • @Victoria, thanks. If you use a return statement, you should remove the break after it, since return exits the block of code. To help avoid errors, the Java compiler doesn't accept code it recognizes as unreachable (even if just a break statement).

  • You can make an String array and a new String after which you can do the following if for example num == 1 then assign the value of array[0] which is "one" in which case the new String equals "one". For too large and too small assign the "too large" or "too small" string from array if num < 1 and if num > 4 assign those strings from array to new String. Close all if and else if statements and return the new String.

    cont...
  • Is there an easier way to do this? For the numbers 1-4 and other conditions, i made an individual if statement to save the string value to a variable. It seems kind of inefficient to write 6 if statements.

  • @alan, you can use a switch case instead. Click on "Featured Answers" above the Hint.

  • Need help
    import java.util.Scanner;

    public class Main {

    public static String doStuff(int num){
        //your code here
        for(int i = 1; i < 5; i++){
    
    cont...
  • My solution is:

    String mes = "";
    String[] onetofour = {"one", "two", "three", "four"};
    for (int i =1; i<onetofour.length+1; i++) {
    if (num== i) {

    cont...
  • public static String doStuff(int num){
        //your code here
        String[] str = {"too small", "one", "two", "three", "four", "too large"};
        if(num < 1){
            return str[0];
        }
        else if(num > 4){
            return str[5];
        }else{
            return str[num];
        }
    }
    
  • My solution and it is correct :
    String result="";
    String [] test = {"one","two","three","four"};
    if (num 4) result= "too large";

    cont...
  • This continuation of "if" and "else" problems doesn't explain how to convert numbers to text. Can you please explain?

  • You need to return a String.

  • Can I get an explanation on how to return a string? Like I said this wasn't covered in previous lessons.

  • Instead of return 5;, you do return "hello"; See String Class for more info.

  • I keep getting errors in the code I didn't write and I haven't touched the boiler plate code.

    import java.util.Scanner;

    public class Main {

    public static String doStuff(int num){
    
    cont...
  • Never mind got it although I don't know if the way I did it is the most efficient. Thanks love this site!!!!

  • what is wrong with this code:

        String number;
            switch(num){
            case 1:
                number= "one";
                break;
            case 2:
                number= "two";
    
    cont...
  • this code works in eclips?

  • I think i have a very good queston ( at least i hope so).

    Why isnt declaring enough en should i initialize the string. so
    String message=""; works ( initializing)
    String message; does not work, why not? for a string it does not mather right. i get it if it was an int but now??

  • What is wrong with this code?

    textDef = ["Too small","One","Two","Tree","Four","Too large"]
    def printText (num):
    if num<1:
        print (textDef[0])
    elif num>4:
        print (textDef[5])
    else:
        print(textDef[num])
    
    printText(2)
    
  • My solution

    if(num < 1) return "too small";
    if(num > 4) return "too large";
    
    String[] words = {"one","two","three","four"};
    return words[num-1];
    
  • def function1(a):
    x=range (1, 5)
    output=""
    thisdict={
    1:"One",
    2:"Two",
    3:"Three",
    4:"Four"
    }

    cont...
  • im not too comfortable with loops just yet but these are my 2 solutions on js:

    /*function convert(a) {
    let num = a
    if (num < 1) {
    return 'too small'

    cont...
  • const word = {
    1: 'one',
    2: 'two',
    3: 'three',
    };

    function convert(i) {
    let num = word[i]
    if ( i < 1 || i > 3) {
    return 'out of range'
    } else {
    return num
    }
    }

Contact Us
Sign in or email us at [email protected]