Skip to content

Instantly share code, notes, and snippets.

@gitaficionado
Created November 29, 2019 18:05
Show Gist options
  • Save gitaficionado/8e9e70be37f9f927f343e610d9b7fd99 to your computer and use it in GitHub Desktop.
Save gitaficionado/8e9e70be37f9f927f343e610d9b7fd99 to your computer and use it in GitHub Desktop.
Write a method that computes the sum of the digits in an integer. Use the following method header:public static int sumDigits(long n) See page P. 215 in the Liang textbook for more informaiton.
public class SumDigitsInteger_06_02 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter a number: ");
long value = input.nextLong();
System.out.println("The sum of digits for " + value +
" is " + sumDigits(value));
}
public static int sumDigits(long n) {
long temp = Math.abs(n);
int sum = __;
while (temp != 0) {
int remainder = (___)(temp % 10);
sum += _________________;
temp = temp / 10;
}
return sum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment