-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumOfDigits2.java
More file actions
42 lines (36 loc) · 1.51 KB
/
SumOfDigits2.java
File metadata and controls
42 lines (36 loc) · 1.51 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
// Write a Java program than read an integer and calculate the sum of its digits
// and write the number of each digit of the sum in English.
import java.util.Scanner;
public class SumOfDigits2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int num = input.nextInt();
int sum = 0;
// Calculate sum of digits
while (num > 0) {
int digit = num % 10;
sum += digit;
num = num / 10;
}
// Convert sum to English words
String[] ones = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
String[] tens = {"", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
String[] teens = {"", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
String english = "";
if (sum == 0) {
english = "zero";
} else if (sum < 10) {
english = ones[sum];
} else if (sum < 20) {
english = teens[sum % 10];
} else if (sum < 100) {
english = tens[sum / 10] + " " + ones[sum % 10];
} else if (sum == 100) {
english = "one hundred";
}
// Write the result
System.out.println("Sum of digits: " + sum);
System.out.println("Number of each digit of the sum in English: " + english);
}
}