The following code is for calculating the sum of digits anddisplaying the highes
ID: 3615359 • Letter: T
Question
The following code is for calculating the sum of digits anddisplaying the highest and lowest value. I could display the sumand highest value, but the lowest value did not display. Pleasehelp me to display the lowest value. Any answer will beappreciated!!! Thank you!!import java.util.*;
public class SumDigits
{
public static void main(String[] args)
{
// Create a Scanner object toread keyboard input.
Scanner keyboard = newScanner(System.in);
// Get number.
System.out.print("EnterNumber:");
String digits =keyboard.nextLine();
int[] a = newint[digits.length()];
int max = 0;
int min = 0;
int sum = 0;
for (int i = 0; i <digits.length(); i++)
{
a[i] =Integer.parseInt(digits.substring(i,i+1));
if(a[i]> max)
max = a[i];
elseif(a[i] < min)
min = a[i];
sum = sum+ a[i];
}
System.out.print("The sum ofthe digits is: ");
System.out.println(sum);
System.out.println("Thelowest digit is " + min);
System.out.println("Thehighest digit is " + max);
}
}
Explanation / Answer
import java.util.*; public class highAndLow { public static void main(String args[]){ Scanner keyboard = newScanner(System.in); String str; int digits[]; int sum = 0; System.out.println("Pleaseenter in the string."); str = keyboard.next(); digits = newint[str.length()]; for(int i = 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.