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

后端 未结 8 545
名媛妹妹
名媛妹妹 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 12:14

    /*Maximum value that can be entered is 2,147,483,647
     * Program to convert entered number into string
     * */
    import java.util.Scanner;
    
    public class NumberToWords 
    {
    
    public static void main(String[] args) 
    {
        double num;//for taking input number
        Scanner obj=new Scanner(System.in);
        do
        {
            System.out.println("\n\nEnter the Number (Maximum value that can be entered is 2,147,483,647)");
            num=obj.nextDouble();
            if(num<=2147483647)//checking if entered number exceeds maximum integer value
            {
                int number=(int)num;//type casting double number to integer number
                splitNumber(number);//calling splitNumber-it will split complete number in pairs of 3 digits
            }
            else
                System.out.println("Enter smaller value");//asking user to enter a smaller value compared to 2,147,483,647
        }while(num>2147483647);
    }
    //function to split complete number into pair of 3 digits each
    public static void splitNumber(int number)
    {   //splitNumber array-contains the numbers in pair of 3 digits
        int splitNumber[]=new int[4],temp=number,i=0,index;
        //splitting number into pair of 3
        if(temp==0)
            System.out.println("zero");
        while(temp!=0)
        {
            splitNumber[i++]=temp%1000;
            temp/=1000;
        }
        //passing each pair of 3 digits to another function
        for(int j=i-1;j>-1;j--)
        {   //toWords function will split pair of 3 digits to separate digits
            if(splitNumber[j]!=0)
                {toWords(splitNumber[j]);
            if(j==3)//if the number contained more than 9 digits
                System.out.print("billion,");
            else if(j==2)//if the number contained more than 6 digits & less than 10 digits
                System.out.print("million,");
            else if(j==1)
                System.out.print("thousand,");//if the number contained more than 3 digits & less than 7 digits
                }
                }       
    }
    //function that splits number into individual digits
    public static void toWords(int number)
        //splitSmallNumber array contains individual digits of number passed to this function
    {   int splitSmallNumber[]=new int[3],i=0,j;
        int temp=number;//making temporary copy of the number
        //logic to split number into its constituent digits
    
        while(temp!=0)
        {
            splitSmallNumber[i++]=temp%10;
            temp/=10;
        }
        //printing words for each digit
        for(j=i-1;j>-1;j--)
          //{   if the digit is greater than zero
            if(splitSmallNumber[j]>=0)
                //if the digit is at 3rd place or if digit is at (1st place with digit at 2nd place not equal to zero)
            {   if(j==2||(j==0 && (splitSmallNumber[1]!=1)))
                {
                    switch(splitSmallNumber[j])
                    {
    
                        case 1:System.out.print("one ");break;
                        case 2:System.out.print("two ");break;
                        case 3:System.out.print("three ");break;
                        case 4:System.out.print("four ");break;
                        case 5:System.out.print("five ");break;
                        case 6:System.out.print("six ");break;
                        case 7:System.out.print("seven ");break;
                        case 8:System.out.print("eight ");break;
                        case 9:System.out.print("nine ");break;
    
                }
    
            }
            //if digit is at 2nd place
            if(j==1)
            {       //if digit at 2nd place is 0 or 1
                    if(((splitSmallNumber[j]==0)||(splitSmallNumber[j]==1))&& splitSmallNumber[2]!=0 )
                    System.out.print("hundred ");
                switch(splitSmallNumber[1])
                {   case 1://if digit at 2nd place is 1 example-213
                            switch(splitSmallNumber[0])
                            {
                            case 1:System.out.print("eleven ");break;
                            case 2:System.out.print("twelve ");break;
                            case 3:System.out.print("thirteen ");break;
                            case 4:System.out.print("fourteen ");break;
                            case 5:System.out.print("fifteen ");break;
                            case 6:System.out.print("sixteen ");break;
                            case 7:System.out.print("seventeen ");break;
                            case 8:System.out.print("eighteen ");break;
                            case 9:System.out.print("nineteen ");break;
                            case 0:System.out.print("ten ");break;
                            }break;
                            //if digit at 2nd place is not 1
                        case 2:System.out.print("twenty ");break;
                        case 3:System.out.print("thirty ");break;
                        case 4:System.out.print("forty ");break;
                        case 5:System.out.print("fifty ");break;
                        case 6:System.out.print("sixty ");break;
                        case 7:System.out.print("seventy ");break;
                        case 8:System.out.print("eighty ");break;
                        case 9:System.out.print("ninety ");break;
                        //case 0:   System.out.println("hundred ");break;
    
                }                           
            }           
        }
      }
    
    }
    

提交回复
热议问题