Computer Science, asked by vishnuraj4151, 3 months ago

write a program to convert a number entered by the user into its corresponding number in words help!!​

Answers

Answered by tseries12345678901
0

Answer:

//This program can be used to convert a number to words

import java.util.*;

class NumberToWords

{

private static final String EMPTY = “”;

private static final String[] X =

{

EMPTY, “One “, “Two “, “Three “, “Four “, “Five “, “Six “,

“Seven “, “Eight “, “Nine “, “Ten “, “Eleven “,”Twelve “,

“Thirteen “, “Fourteen “, “Fifteen “, “Sixteen “,

“Seventeen “, “Eighteen “, “Nineteen “

};

private static final String[] Y =

{

EMPTY, EMPTY, “Twenty “, “Thirty “, “Forty “, “Fifty “,

“Sixty “, “Seventy “, “Eighty “, “Ninety “

};

private static String convertToDigit(int n, String suffix)

{

if (n == 0) {

return EMPTY;

}

if (n > 19) {

return Y[n / 10] + X[n % 10] + suffix;

}

else {

return X[n] + suffix;

}

}

public static String convert(int n)

{

StringBuilder res = new StringBuilder();

res.append(convertToDigit(((n / 100000) % 100), “Lakh, “));

res.append(convertToDigit(((n / 1000) % 100), “Thousand “));

res.append(convertToDigit(((n / 100) % 10), “Hundred “));

if ((n > 100) && (n % 100 != 0)) {

res.append(“and “);

}

res.append(convertToDigit((n % 100), “”));

return res.toString();

}

public static void main(String[] args)

{

int n;

System.out.println(“\nEnter a number : “);

Scanner sc = new Scanner(System.in);

n = sc.nextInt();

System.out.println(convert(n));

}

}

Langauge: Java

Output: (Taking 1289 as example)

Enter a Number :

1289

Words: One Thousand Two Hundred and Eighty Nine

Similar questions