-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDigit_Sum_in_Words.java
More file actions
55 lines (45 loc) · 1.88 KB
/
Copy pathDigit_Sum_in_Words.java
File metadata and controls
55 lines (45 loc) · 1.88 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.io.*;
public class Digit_Sum_in_Words {
public static void main(String[] args) {
// Create a BufferedReader to read input from the user
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
int sum = 0;
// Read a line of text from the user
String str = br.readLine();
char[] numStr = str.toCharArray();
// Calculate the sum of the digits in the input number
for (int i = 0; i < numStr.length; i++) {
sum += numStr[i] - '0';
}
// Print the original number and call the print_number function
System.out.println("Original Number: " + str);
print_number(sum);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void print_number(int n) {
int x;
int y;
int z;
String[] number = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
// Print the sum of the digits of the number
System.out.println("Sum of the digits of the said number: " + n);
if (n < 10) {
// If the number is less than 10, print the corresponding word
System.out.println(number[n]);
} else if (n < 100) {
// If the number is less than 100, split it into tens and ones
x = n / 10;
y = n - x * 10;
System.out.println("In English: " + number[x] + " " + number[y]);
} else {
// If the number is three digits, split it into hundreds, tens, and ones
x = n / 100;
y = (n - x * 100) / 10;
z = n - x * 100 - y * 10;
System.out.println("In English: " + number[x] + " " + number[y] + " " + number[z]);
}
}
}