-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintValue.java
More file actions
34 lines (28 loc) · 1009 Bytes
/
Copy pathPrintValue.java
File metadata and controls
34 lines (28 loc) · 1009 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.Scanner;
class PrintValue {
// Function to print the word for the corresponding digit
static void printWord(String N) {
// Array to store words for digits 0-9
String[] words = { "Zero", "One", "Two", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine" };
// Iterate through each character in the string
for (char digit : N.toCharArray()) {
if (Character.isDigit(digit)) {
// Print the word for the digit
System.out.print(words[digit - '0'] + " ");
} else {
System.out.print("Invalid Input ");
}
}
System.out.println();
}
// Driver code
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Prompt the user for input
System.out.print("Enter a number: ");
String N = sc.nextLine();
// Call the function to print words
printWord(N);
}
}