Using the JOptionPane class w rite a program that prompts the user to input an i
ID: 664139 • Letter: U
Question
Using the JOptionPane class write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example, the program should: ouput the individual digits of 3456 as 3 4 5 6 and the sum as 18, output the individual digits of 8030 as 8 0 3 0 and the sum as 11, output the individual digits of 2345526 as 2 3 4 5 5 2 6 and the sum as 27, output the individual digits of 4000 as 4 0 0 0 and the sum as 4, and output the individual digits of -2345 as 2 3 4 5 and the sum as 14.
Thanks in advance for your help.
Explanation / Answer
import java.util.*
public Class JOptionPane
{
public static void main(String [ ] args)
{
do
{
int adigit, aNum, thesum=0;
string astringNum;
char digitAt;
scanner console = new Scanner(system.in);
system.out.println("please enter an integerc");
aNum = console.nextInt();
astringNum = String.valueof(aNum);
for(int count =0; count < aStringNum.length(); count++)
{
digitAt = aStringNum.charAt(count);
adigit = character .getnumericvalue(digitAt);
thesum + =aDigit;
system.out.println(aDigit +");
}
system.out.println("the sum is "+thesum);
}while(num>0);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.