Armstrong.cpp
Server-Architecture-Firmware/Armstrong.cpp
#include <iostream>
#include <cmath>
using namespace std;
bool isArmstrong(int n) {
int original = n;
int sum = 0;
int digits = to_string(n).length();
while (n > 0) {
int digit = n % 10;
sum += pow(digit, digits);
n /= 10;
}
return sum == original;
}
int main() {
int num = 153;
cout << num << " is Armstrong? " << boolalpha << isArmstrong(num) << endl;
num = 9474;
cout << num << " is Armstrong? " << boolalpha << isArmstrong(num) << endl;
num = 123;
cout << num << " is Armstrong? " << boolalpha << isArmstrong(num) << endl;
return 0;
}
Articoli correlati
MaxOfTwo.cpp
MaxOfTwo.cpp — cpp source code from the Server-Architecture-Firmware learning materials (Server-Architecture-Firmware/MaxOfTwo.cpp).
Leggi l'articolo →RTTI.cpp
RTTI.cpp — cpp source code from the Server-Architecture-Firmware learning materials (Server-Architecture-Firmware/RTTI.cpp).
Leggi l'articolo →console_input.cpp
console_input.cpp — cpp source code from the Server-Architecture-Firmware learning materials (Server-Architecture-Firmware/console_input.cpp).
Leggi l'articolo →function.cpp
function.cpp — cpp source code from the Server-Architecture-Firmware learning materials (Server-Architecture-Firmware/function.cpp).
Leggi l'articolo →main.cpp
main.cpp — cpp source code from the Server-Architecture-Firmware learning materials (Server-Architecture-Firmware/main.cpp).
Leggi l'articolo →manyifelse.cpp
manyifelse.cpp — cpp source code from the Server-Architecture-Firmware learning materials (Server-Architecture-Firmware/manyifelse.cpp).
Leggi l'articolo →