Write a Java program that prompts the user for and accepts as input a 5-digit in
ID: 3600532 • Letter: W
Question
Write a Java program that prompts the user for and accepts as input a 5-digit integer value. The program should then calculate and print the sum of the individual digits of the number. For example, if the user inputs "53291", your program should add together the individual digits of the number and then print "20 as their sum. If the user inputs "12345", your program should then print the value "15 as their sum. Make sure your program prompts the user for input with an appropriate descriptive message, and also provides an appropriate message to describe its output.Explanation / Answer
FiveDigitSum.java
import java.util.Scanner;
public class FiveDigitSum {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter 5-digit integer: ");
int n = scan.nextInt();
int sum = 0;
while(n > 0) {
sum= sum + (n % 10);
n = n / 10;
}
System.out.println("Sum of digits is: "+sum);
}
}
Output:
Enter 5-digit integer:
12345
Sum of digits is: 15