# Rookie here, need help with sum of digits program

Printable View

• February 12th, 2013, 04:06 PM
wkellogg10
Rookie here, need help with sum of digits program
I am completely new to java and am very lost on a current program I am working on. I cannot figure out what code to add in or use when it comes to extracting the digits using an operator.

Here is my instructions:

Write a program that reads an integer from 0 to 999 from the keyboard and then adds up all the digits in the integer. For example, if the integer is 735 then the sum of the digits is 7+3+5=15.

(This is the current code I have so far) Any help is appreciated.

Code Java:

```1 import java.util.Scanner; 2 /** 3 * 4 * @author willkellogg 5 */ 6 public class SumDigits { 7 public static void main(String[] args) { 8 Scanner input = new Scanner(System.in) ; 9 10 //input 11 System.out.println("Enter a number from 0 to 999: ") ; 12 double digit1 = input.nextDouble() ; 13 double digit2 = input.nextDouble() ; 14 double digit3 = input.nextDouble() ; 15 16 //process 17 double average = (digit1 + digit2 + digit3) / 3 ; 18 19 20 //output 21 System.out.println("The sum of the digit is " + digit1 + "" + digit2 + "" 22 + digit3 + "is" + average) ;       } }```
• February 12th, 2013, 05:00 PM
Norm
Re: Rookie here, need help with sum of digits program
Your code is not following the assigment: program that reads an integer
The first fix for the program is to change what it reads from three doubles to one int.

The trick for isolating digits in a number is by using the % and / operators:
132 % 10 = 2
132 / 10 = 13
• February 12th, 2013, 06:25 PM
javaiscool
Re: Rookie here, need help with sum of digits program
.