Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- int armstrong_method(int, size_t);
- int main() {
- int n;
- try {
- cout << "Vuvedi n: ";
- cin >> n;
- if (!cin || n < 2) {
- throw "Error: Invalid number";
- }
- size_t count((size_t)log10((double)n) + 1);
- if (armstrong_method(n, count) == n)
- cout << "Armstrong number: " << armstrong_method(n, count) << endl;
- else
- cout << "Not an Armstrong number, but here is the sum: " << armstrong_method(n, count) << endl;
- }
- catch (const exception &error) {
- cout << error.what();
- }
- return 0;
- }
- int armstrong_method(int n, size_t power) { return n ? (pow(n % 10, power) + armstrong_method(n / 10, power)) : 0; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement