sum the user input


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: sum the user input

  1. #1
    Join Date
    Jun 2003
    Posts
    1

    Question sum the user input

    Hi guys, how can i sum number from the user input. For example if user input is "456" the output(system print) would be "15" (4+5+6). How can i do that?

    i already have this following code:

    import java.io.*;
    public class Sum
    {
    public static void main(String[]args)throws IOException
    {
    BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter an integer between 0 and 999: ");
    int number = Integer.parseInt(keyboard.readLine());

    Thanks.

  2. #2
    Join Date
    Mar 2003
    Posts
    86

    Simple loop

    Just convert to string, and loop over it char by char. Add all chars are ints.

    Sharbov.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center