Need to see what\'s missing from my program..... Write a program that prompts th
ID: 3557711 • Letter: N
Question
Need to see what's missing from my program.....
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.
Here is my program: I keep getting an error "reached end of file while parsing"
import java.util.*;
public class SumofDigits
{
static Scanner console=new Scanner(System.in);
public static void main(String []args)
{
int num1, test, rem;
int counter = 0;
System.out.print("Enter a number: ");
um1=console.nextInt();
test = num1;
while (test > 0)
{
test = test/10;
counter = counter + 1;
}
counter = counter - 1;
while (counter >= 0)
{
rem = num1% (int) Math.pow(10, counter);
num1 = num1/(int) Math.pow(10,counter);
System.out.print (num1 + " ");
num1 = rem;
counter = counter - 1;
}
Explanation / Answer
import java.util.*;
public class SumofDigits
{
static Scanner console=new Scanner(System.in);
public static void main(String []args)
{
int num1, test, rem;
int sum =0;
int counter = 0;
Scanner console = new Scanner(System.in); // this variable is missing.
System.out.print("Enter a number: ");
num1=console.nextInt(); // here not um1 it is num1.
test = num1;
while (test > 0)
{
test = test/10;
counter = counter + 1;
}
counter = counter - 1;
while (counter >= 0)
{
rem = num1% (int) Math.pow(10, counter);
num1 = num1/(int) Math.pow(10,counter);
System.out.print (num1 + " ");
sum+= num1; // add each digit to sum;
num1 = rem;
counter = counter - 1;
}
System.out.println(" Sum of Digits is "+sum); // this line is missing
} // this bracket is misisng
}// this bracket is misisng
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.