fork download
  1. /*
  2. File: SumOfDigits.java
  3. Author: Aaron Champagne
  4. Date: January 19, 2013
  5. Purpose: User provides a 3-digit number, calculate sum of the digits.
  6. */
  7. import java.util.Scanner;
  8. public class SumOfDigits {
  9. public static void main (String[] args) {
  10.  
  11. Scanner input = new Scanner(System.in);
  12.  
  13. //declare variables
  14. int originalNum;
  15. int digit1;
  16. int digit2;
  17. int digit3;
  18. int sum;
  19.  
  20. //Prompt user for input
  21. System.out.print("Please enter a three-digit number: ");
  22. originalNum = Integer.parseInt(input.nextLine());
  23.  
  24. //Extract each digit
  25. digit1 = originalNum / 100;
  26. digit2 = originalNum /10;
  27. digit2 = digit2 % 10;
  28. digit3 = originalNum % 10;
  29.  
  30. //Calculate sum
  31. sum = digit1 + digit2 + digit3;
  32.  
  33. //Display results
  34. System.out.println("Your original number was " + originalNum + " and the sum the digits is " + sum + '.');
  35. }
  36. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout

Standard output is empty