Write code to convert given number into words (eg 1234 as input should output one thousand two hundred and thirty four)

后端 未结 8 552
名媛妹妹
名媛妹妹 2021-02-01 11:37

Write C/C++/Java code to convert given number into words.

eg:- Input: 1234

Output: One thousand two hundred thirty-four.

Input: 10

Output: Ten

相关标签:
8条回答
  • 2021-02-01 11:52
    #include<iostream>
    using namespace std;
    void expand(int);
    int main()
    {
        int num;
        cout<<"Enter a number : ";
        cin>>num;
        expand(num);
    }
    void expand(int value)
    {
        const char * const ones[20] = {"zero", "one", "two", "three","four","five","six","seven",
        "eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen",
        "eighteen","nineteen"};
        const char * const tens[10] = {"", "ten", "twenty", "thirty","forty","fifty","sixty","seventy",
        "eighty","ninety"};
    
        if(value<0)
        {
            cout<<"minus ";
            expand(-value);
        }
        else if(value>=1000)
        {
            expand(value/1000);
            cout<<" thousand";
            if(value % 1000)
            {
                if(value % 1000 < 100)
                {
                    cout << " and";
                }
                cout << " " ;
                expand(value % 1000);
            }
        }
        else if(value >= 100)
        {
            expand(value / 100);
            cout<<" hundred";
            if(value % 100)
            {
                cout << " and ";
                expand (value % 100);
            }
        }
        else if(value >= 20)
        {
            cout << tens[value / 10];
            if(value % 10)
            {
                cout << " ";
                expand(value % 10);
            }
        }
        else
        {
            cout<<ones[value];
        }
        return;
    }
    
    0 讨论(0)
  • 2021-02-01 11:55

    Instead of switch statements, consider using tables of strings indexed by a small value.

    const char * const ones[20] = {"zero", "one", "two", ..., "nineteen"};
    const char * const tens[10] = {"", "ten", "twenty", ..., "ninety"};
    

    Now break the problem into small pieces. Write a function that can output a single-digit number. Then write a function that can handle a two-digit number (which will probably use the previous function). Continue building up the functions as necessary.

    Create a list of test cases with expected output, and write code to call your functions and check the output, so that, as you fix problems for the more complicated cases, you can be sure that the simpler cases continue to work.

    0 讨论(0)
  • 2021-02-01 11:57
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int len=0,revnum,i,dup=0,j=0,k=0;
    
     long int gvalue;
    
     char  ones[]  [10]={"one","Two","Three","Four","Five","Six","Seven","Eight","Nine","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen",""};
    
     char twos[][10]={"Ten","Twenty","Thirty","Fourty","fifty","Sixty","Seventy","eighty","Ninety",""};
    
    clrscr();
    
    
     printf("\n Enter value"); 
    
     scanf("%ld",&gvalue);
     if(gvalue==10)
    
       printf("Ten");
    
     else if(gvalue==100)
    
       printf("Hundred");
    
     else if(gvalue==1000)
    
       printf("Thousand");
    
     dup=gvalue;
    
     for(i=0;dup>0;i++)
      {
       revnum=revnum*10+dup%10;
    
       len++;
    
       dup=dup/10;
    
      }
    
     while(j<len)
    
     {
    
     if(gvalue<10)
    
      {
    
       printf("%s ",ones[gvalue-1]);
    
       }
    
     else if(gvalue>10&&gvalue<=19)
    
      {
    
       printf("%s ",ones[gvalue-2]);
    
       break;
    
      }
    
     else if(gvalue>19&&gvalue<100)
    
      {
    
       k=gvalue/10;
    
       gvalue=gvalue%10;
    
        printf("%s ",twos[k-1]);
    
       }
    
      else if(gvalue>100&&gvalue<1000)
    
       {
    
        k=gvalue/100;
    
        gvalue=gvalue%100;
    
        printf("%s Hundred ",ones[k-1]);
    
         }
    
       else if(gvalue>=1000&&gvlaue<9999)
    
       {
    
        k=gvalue/1000;
    
        gvalue=gvalue%1000;
    
        printf("%s Thousand ",ones[k-1]);
    
        }
    
       else if(gvalue>=11000&&gvalue<=19000)
    
        {
    
        k=gvalue/1000;
    
        gvalue=gvalue%1000;
    
        printf("%s Thousand ",twos[k-2]);
    
        }
    
       else if(gvalue>=12000&&gvalue<100000)
    
       {
    
        k=gvalue/10000;
    
        gvalue=gvalue%10000;
    
        printf("%s ",ones[gvalue-1]);
    
       }
    
    else
    
    {
    
    printf("");
    
    }
    
     j++;
    
     getch();
    
    }
    
    0 讨论(0)
  • 2021-02-01 11:58

    Works for any number from 0 to 999999999.

    This program gets a number from the user, divides it into three parts and stores them separately in an array. The three numbers are passed through a function that convert them into words. Then it adds "million" to the first part and "thousand" to the second part.

    #include <iostream>
    using namespace std;
    int buffer = 0, partFunc[3] = {0, 0, 0}, part[3] = {0, 0, 0}, a, b, c, d;
    long input, nFake = 0;
    const char ones[][20] = {"",       "one",       "two",      "three",
                             "four",    "five",      "six",      "seven",
                             "eight",   "nine",      "ten",      "eleven",
                             "twelve",  "thirteen",  "fourteen", "fifteen",
                             "sixteen", "seventeen", "eighteen", "nineteen"};
    const char tens[][20] = {"",     "ten",   "twenty",  "thirty", "forty",
                             "fifty", "sixty", "seventy", "eighty", "ninety"};
    void convert(int funcVar);
    int main() {
      cout << "Enter the number:";
      cin >> input;
      nFake = input;
      buffer = 0;
      while (nFake) {
        part[buffer] = nFake % 1000;
        nFake /= 1000;
        buffer++;
      }
      if (buffer == 0) {
        cout << "Zero.";
      } else if (buffer == 1) {
        convert(part[0]);
      } else if (buffer == 2) {
        convert(part[1]);
        cout << " thousand,";
        convert(part[0]);
      } else {
        convert(part[2]);
        cout << " million,";
    
        if (part[1]) {
          convert(part[1]);
          cout << " thousand,";
        } else {
          cout << "";
        }
        convert(part[0]);
      }
      system("pause");
      return (0);
    }
    
    void convert(int funcVar) {
      buffer = 0;
      if (funcVar >= 100) {
        a = funcVar / 100;
        b = funcVar % 100;
        if (b)
          cout << " " << ones[a] << " hundred and";
        else
          cout << " " << ones[a] << " hundred ";
        if (b < 20)
          cout << " " << ones[b];
        else {
          c = b / 10;
          cout << " " << tens[c];
          d = b % 10;
          cout << " " << ones[d];
        }
      } else {
        b = funcVar;
        if (b < 20)
          cout << ones[b];
        else {
          c = b / 10;
          cout << tens[c];
          d = b % 10;
          cout << " " << ones[d];
        }
      }
    }
    
    0 讨论(0)
  • 2021-02-01 12:01
    /* This Program will convert Numbers from -999,999,999 to 999,999,999 into words */
    
    #include <vector>
    #include <iostream>
    #include <stdexcept>
    #include <string>
    
    using namespace std;
    
    const std::vector<std::string> first14 = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen" };
    const std::vector<std::string> prefixes = { "twen", "thir", "for", "fif", "six", "seven", "eigh", "nine" };
    
    std::string inttostr(const int number)
    {
        if (number < 0)
        {
            return "minus " + inttostr(-number);
        }
        if (number <= 14)
            return first14.at(number);
        if (number < 20)
            return prefixes.at(number - 12) + "teen";
        if (number < 100) {
            unsigned int remainder = number - (static_cast<int>(number / 10) * 10);
            return prefixes.at(number / 10 - 2) + (0 != remainder ? "ty " + inttostr(remainder) : "ty");
        }
        if (number < 1000) {
            unsigned int remainder = number - (static_cast<int>(number / 100) * 100);
            return first14.at(number / 100) + (0 != remainder ? " hundred " + inttostr(remainder) : " hundred");
        }
        if (number < 1000000) {
            unsigned int thousands = static_cast<int>(number / 1000);
            unsigned int remainder = number - (thousands * 1000);
            return inttostr(thousands) + (0 != remainder ? " thousand " + inttostr(remainder) : " thousand");
        }
        if (number < 1000000000) {
            unsigned int millions = static_cast<int>(number / 1000000);
            unsigned int remainder = number - (millions * 1000000);
            return inttostr(millions) + (0 != remainder ? " million " + inttostr(remainder) : " million");
        }
        throw std::out_of_range("inttostr() value too large");
    }
    
    int main()
    {
        int num;
        cout << "Enter a number to convert it into letters : ";
        cin >> num;
        cout << endl << num << " = " << inttostr(num) << endl;
        system("pause");
        return 0;
    }
    
    0 讨论(0)
  • 2021-02-01 12:07
    import java.lang.*;
    import java.io.*;
    public class rupee
    {
    public static void main(String[] args)throws  IOException
    {
    
    int len=0,revnum=0,i,dup=0,j=0,k=0;
    int gvalue;
     String[] ones={"one","Two","Three","Four","Five","Six","Seven","Eight","Nine","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen",""};
    String[] twos={"Ten","Twenty","Thirty","Fourty","fifty","Sixty","Seventy","eighty","Ninety",""};
    System.out.println("\n Enter value");
    InputStreamReader b=new InputStreamReader(System.in);
    BufferedReader br=new BufferedReader(b);
    gvalue=Integer.parseInt(br.readLine());
    if(gvalue==10)
    
       System.out.println("Ten");
    
    else if(gvalue==100)
    
      System.out.println("Hundred");
    
    else if(gvalue==1000)
      System.out.println("Thousand");
    
    dup=gvalue;
    for(i=0;dup>0;i++)
    {
      revnum=revnum*10+dup%10;
      len++;
      dup=dup/10;
    }
    while(j<len)
    {
     if(gvalue<10)
     {
      System.out.println(ones[gvalue-1]);
     }
    else if(gvalue>10&&gvalue<=19)
     {
      System.out.println(ones[gvalue-2]);
      break;
     }
    else if(gvalue>19&&gvalue<100)
     {
       k=gvalue/10;
       gvalue=gvalue%10;
       System.out.println(twos[k-1]);
     }
    else if(gvalue>100&&gvalue<1000)
     {
       k=gvalue/100;
       gvalue=gvalue%100;
       System.out.println(ones[k-1] +"Hundred");
     }
    else if(gvalue>=1000&&gvalue<9999)
     {
       k=gvalue/1000;
       gvalue=gvalue%1000;
       System.out.println(ones[k-1]+"Thousand");
    }
    else if(gvalue>=11000&&gvalue<=19000)
     {
      k=gvalue/1000;
      gvalue=gvalue%1000;
      System.out.println(twos[k-2]+"Thousand");
     }      
    else if(gvalue>=12000&&gvalue<100000)
    {
     k=gvalue/10000;
         gvalue=gvalue%10000;
     System.out.println(ones[gvalue-1]);
    }
    else
    {
     System.out.println("");
        }
    j++;
    }
    }
    }
    
    0 讨论(0)
提交回复
热议问题