Write a program that prompts the user to input an integer an

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. Using a while loop with a flag must use a substring for the numbers and sum.

I am so new at this I don\'t even know where to begin I was lost at the while loop and the substring.

import java.util.*;

public class sumDigits
{
static Scanner console = new Scanner(System.in);

public static void main(String[] args)
{
//declare variables
int number, condition, remainder;
int counter = 0;
int sum = 0;
  
//get input
System.out.println(\"Please enter an integer: \");
number = console.nextInt();
  
for (x = 0; x < var.length; x++)
{
numberStr = var.substring(x, x + 1);
number = Interger.parseInt(numberStr);
sum = sum + number;
}
System.out.print(\"\ The sum of the digits is \" + sum);
  
}//end main
}//end class

Solution

Here is code:

import java.util.*;
public class sumDigits
{
static Scanner console = new Scanner(System.in);

public static void main(String[] args)
{
//declare variables
int number, condition, remainder,temp;
int counter = 0;
int sum = 0;
  
//get input
System.out.println(\"Please enter an integer: \");
number = console.nextInt();
if(number < 0) // is number is negitive
{
number = -(number); // converts to positive
}
  
String numbers = \"\";
while (number > 0) {
temp = number;
sum = sum + number % 10; //Extracts the last digit and adds it to the sum
number = number / 10; //removes the last digit
numbers = (temp % 10) + \" \" + numbers;
}

System.out.println(numbers + \"and the sum is \" + sum);
  
}//end main
}//end class

Output

Please enter an integer:                                                                                                 

-5462                                                                                                                    

5 4 6 2 and the sum is 17

case 2

Please enter an integer:                                                                                                 

51564                                                                                                                    

5 1 5 6 4 and the sum is 21

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
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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site