bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

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

Java How To Check Armstrong Number

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Java How To Check Armstrong Number?

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.

___ num = 153;
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Explanation: An Armstrong number means: take each digit, raise it to the power of the number of digits, and add them together.
An Armstrong number is equal to the sum of its digits raised to the power of the number of digits (e.
Check Armstrong Number

Check Armstrong Number

An Armstrong number is equal to the sum of its digits raised to the power of the number of digits (e.g. 153).

Example

int num = 153;
int original = num;
int result = 0;
int digits = String.valueOf(num).length();
while (num != 0) {
  int digit = num % 10;
  result += Math.pow(digit, digits);
  num /= 10;
}
System.out.println(original + (result == original ? " is Armstrong" : " is not Armstrong"));

Explanation: An Armstrong number means: take each digit, raise it to the power of the number of digits, and add them together. For 153 (3 digits): - First digit: 1³ = 1 - Second digit: 5³ = 125 - Third digit: 3³ = 27 Now add them: 1 + 125 + 27 = 153 . Since the sum is the same as the original number, 153 is an Armstrong number.

Previous

Java How To - Sum of Digits

Next

Java How To Generate Random Numbers