Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › MATH BASICS
An Armstrong number equals the sum of each of its digits raised to the total number of digits. The number 1634 has four digits, so its test is 1^4 + 6^4 + 3^4 + 4^4, not a sum of cubes. The exponent comes from counting the digits in the number being tested.
Use a working copy to count the digits. Each division by 10 removes the last digit, so the working copy takes the values 1634, 163, 16, 1, and 0. The number of removals is 4, which becomes the exponent for every digit.
int original = 1634;
int work = original;
int digits = 0;
while (work > 0) {
digits++;
work /= 10;
}
// digits is now 4Which exponent must every digit of 1634 use in the Armstrong test?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The digit-counting loop consumes its working copy until that copy becomes 0. For 1634, original remains 1634 while work changes from 1634 to 163 to 16 to 1 and finally to 0. The saved original is needed later, because the final decision compares the calculated sum with the number you started with.
After counting, restore work from original before beginning the sum pass. The two passes need separate jobs: the first consumes work to find the exponent, and the second consumes a fresh copy to extract every digit. Keeping original unchanged gives both passes a reliable source value and gives the final comparison the correct target.
int original = 1634;
int work = original;
int digits = 0;
while (work > 0) {
digits++;
work /= 10;
}
work = original;
int sum = 0;
// The extraction pass uses work from here.During the second pass, the last digit is extracted first, so 1634 produces the order 4, 3, 6, 1. Each digit starts with a contribution of 1 and is multiplied by itself four times. This computes the fourth power using integer arithmetic, without depending on a floating-point power result.
int digit = work % 10;
int contribution = 1;
for (int j = 0; j < digits; j++) {
contribution *= digit;
}
sum += contribution;
work /= 10;| EXTRACTED DIGIT | INTEGER POWER | RUNNING SUM |
|---|---|---|
| 4 | 4^4 = 256 | 256 |
| 3 | 3^4 = 81 | 337 |
| 6 | 6^4 = 1296 | 1633 |
| 1 | 1^4 = 1 | 1634 |
The counting pass has consumed work. Restore the missing statement before the extraction pass begins.
int original = 1634;
int work = original;
int digits = 0;
while (work > 0) {
digits++;
work /= 10;
}
// Restore work here
int sum = 0;Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
After every digit has contributed, the sum is 1634. The saved original is also 1634, so the equality check succeeds and 1634 is an Armstrong number. There is no extra condition after this comparison: equal means the number satisfies the rule, and unequal means it does not.
Both digit passes use constant extra space. They store the original, the working copy, the digit count, the current digit, the contribution, and the sum, regardless of how many digits the number has. The repeated multiplication performs one multiplication for each digit and for each counted digit position, so its work grows with the digit count.
if (sum == original) {
// 1634 is an Armstrong number
} else {
// 1634 is not an Armstrong number
}