bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java How To's
Java•Java How To's

Java How To - Sum of Digits

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Java How To - Sum of Digits?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ n = 352;

Sum of Digits of a Number

Add up all digits (e.g., 352: 3 + 5 + 2 = 10):

Example

int n = 352;
int sum = 0;
while (n > 0) {
  sum += n % 10;
  n /= 10;
}
System.out.println("Sum of digits: " + sum);

Explanation: We use n % 10 to extract the last digit, add it to the sum, and then remove the digit with n /= 10 .

Previous

Java How To - Convert Celsius to Fahrenheit

Next

Java How To Check Armstrong Number