...

/

Solution Review: Sum of Digits in an Integer

Solution Review: Sum of Digits in an Integer

In this review, solution of the challenge 'Sum of Digits in an Integer' from the previous lesson is provided.

We'll cover the following...

Solution

Press + to interact
class SumDigits {
public static int sumOfDig(int var) {
int result = 0; //variable for resultant sum
int lastDigit = 0;
while (var > 0) {
//seclude & keep adding the last digit into result
lastDigit = var % 10;
result = result + lastDigit;
System.out.println("Last Digit: " + lastDigit);
System.out.println("Sum: " + result);
var /= 10; //update the new value of var
System.out.println("Number: " + var);
}
return result;
}
public static void main( String args[] ) {
int number = 1745;
System.out.println("Number: " + number);
System.out.println( "Sum of digits in 1024 is: "+ sumOfDig(number) );
}
}

Understanding the

...