Answers for "Number To Words java solo"

0

Number To Words java solo

public class Main {

    public static void main(String[] args) {
        System.out.println(reverse(1000));
        System.out.println(reverse(-2));
        System.out.println(getDigitCount(4567));
        numberToWords(1000);
        numberToWords(-12);
        numberToWords(123);
	// write your code here
    }
    public static void numberToWords(int number){
        String numString=""; // initialising an empty string
        if(number<0){
            System.out.println("Invalid Value");
        }
        int numDigits=getDigitCount(number); //calling getDigitCount() method
        int reverseNum=reverse(number); // calling reverse() method
        int testNum=0;
        while (numDigits>0){
            testNum=reverseNum%10;
            reverseNum/=10;
            numDigits-=1;
            if(testNum==0){
                numString+="Zero ";
            }
            else{
                switch(testNum){
                    case 1:
                        numString += "One ";
                        break;
                    case 2:
                        numString += "Two ";
                        break;
                    case 3:
                        numString += "Three ";
                        break;
                    case 4:
                        numString += "Four ";
                        break;
                    case 5:
                        numString += "Five ";
                        break;
                    case 6:
                        numString += "Six ";
                        break;
                    case 7:
                        numString += "Seven ";
                        break;
                    case 8:
                        numString += "Eight ";
                        break;
                    case 9:
                        numString += "Nine ";
                        break;

                }
            }
        }
        System.out.println(numString);
        
    }

    public static int reverse(int number){
        int reverseNum=0;

        while(number!=0){
            int lastDigit=number%10;
            reverseNum=(reverseNum*10)+lastDigit;
            number=number/10;
        }
        return reverseNum;
    }


    public static int getDigitCount(int number){
        int digit=0;
        if(number<0){
            return -1;
        }
        if(number==0){
            digit=1;
        }
        while(number>0){
            number=number/10;
            digit+=1;
        }
        return digit;
    }
}
Posted by: Guest on September-17-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language