#include <iostream>
#include <math.h>
using namespace std;
int main() {
int n, temp, rem, digits=0, sum=0;
cout << "Enter a armstrong number: ";
cin >> n;
temp = n;
digits = (int)log10(n) 1;
while (n != 0) {
rem = n % 10;
sum = sum pow(rem, digits);
n = n / 10;
}
if (temp == sum) {
cout << "yes";
}
else {
cout << "not";
}
}
How does the " digits = (int)log10(n) 1; " line actually calculates the digits? can anyone explain?
CodePudding user response:
Math.
Logarithms are basically "exponents in reverse." Log10(100) is 2.0
, as 10 to the second power is 100.
Cast to 'int
and add one to that are you get 3
, which is the number of digits.