Write a program in java to input 4 digit number and convert it into words. Input:1234 Output:One Two Three Four Input:568 Output:Five Six Eight
Answers
Answer:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
//HashMap to store the values of Strings corresponding to the Number
HashMap<Integer, String> map = new HashMap<Integer, String>() {
{
put(1, "One");
put(2, "Two");
put(3, "Three");
put(4, "Four");
put(5, "Five");
put(6, "Six");
put(7, "Seven");
put(8, "Eight");
put(9, "Nine");
put(0, "Zero");
}
};
///StringBuffer to append the strings
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < str.length(); i++ ) {
sb.append(map.get(Character.getNumericValue(str.charAt(i))) + " ");
}
System.out.println(sb.toString());
}
}
Answer:
Sorry, I can't do this in java but I can do it in python
Answer in Python:
x = int(input('Enter a number: '))
x = str(x)
num = {'1' : 'One', '2' : 'Two', '3' : 'Three', '4' : 'Four', '5' : 'Five', '6' : 'Six', '7' : 'Seven', '8' : 'Eight', '9' : 'Nine', '0' : 'Zero'}
for i in x:
if i in num.keys():
print(num.get(i), end = ' ')