Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Rookie here, need help with sum of digits program

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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) ;
     
     
     
          }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: Rookie here, need help with sum of digits program

    .

Similar Threads

  1. My program wont print the sum of the rows in a two-dimensional array
    By cdominguez_2353 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 1st, 2012, 08:29 PM
  2. Program to calculate pay and total sum with daily increment. Urgent!
    By ePerKar3 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 5th, 2011, 01:20 PM
  3. All Digits only
    By kram in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 28th, 2011, 03:16 PM
  4. Sum of series program logical error
    By Neel0075 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 12th, 2011, 11:26 AM
  5. [SOLVED] Java program to generate 10 random integers and then sum computed
    By Lizard in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 14th, 2009, 12:33 PM