Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*CMPS161
- *Program Assignment 06
- *
- *
- Page 234
- 6.2 (Sum the digits in an integer)
- Write a method that computes the sum of the digits in an integer.
- Use the following method header:
- public static int sumDigits(long n)
- For example, sumDigits(234) returns 9 (2 + 3 + 4).
- Hint:
- Use the % operator to extract digits, and
- use the / operator to remove the extracted digit.
- For instance, to extract 4 from 234, use 234 % 10 (= 4).
- To remove 4 from 234, use 234 / 10 (= 23).
- Use a loop to repeatedly extract and remove the digit until
- all the digits are extracted.
- Write a test program that
- prompts the user to enter an integer and
- displays the sum of all its digits.
- */
- import java.util.Scanner;
- public class Exercise6_2 {
- public static void main(String[] args) {
- // Enter a postive integer: Scanner(System.in)
- Scanner input = new Scanner(System.in);
- System.out.println("Enter a positive integer: ");
- int userInput = input.nextInt();
- //Call method sumDigits and then display the result
- sumDigits (userInput);
- System.out.println(userInput);
- }
- // end of main
- public static int sumDigits(int n) {
- int temp = (int)Math.abs(n); // temp value
- int sum = 0; // the sum of the digits
- while (sum > 0) {
- sum = sum + temp % 10;
- temp = temp / 10;
- System.out.print(sum);
- }
- return sum;
- }
- }
- /*int temp = (int)Math.abs(n); // temp value
- int sum = 0; // the sum of the digits
- // while (loop until all the digits are extracted) {
- // extract a digit (%)
- input % 10
- // add the extracted digit into sum
- // remove the extracted digit (/)
- // }
- // return the sum of the digits
- return sum;
- } // end of sumDigits
- } // end of Exercise6_2
- /* Sample Run
- Enter a number: 234
- The sum of digits for 234 is 9
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement