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:
public class Sum
public static void main(Stringargs)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());
Just convert to string, and loop over it char by char. Add all chars are ints.
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL