-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy patharmstrong_number.cpp
60 lines (53 loc) · 1.26 KB
/
armstrong_number.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <cmath>
bool is_armstrong_number(int n) {
int num_digits = 0;
int temp = n;
while (temp > 0) {
temp /= 10;
num_digits++;
}
int sum_of_powers = 0;
temp = n;
while (temp > 0) {
int digit = temp % 10;
sum_of_powers += pow(digit, num_digits);
temp /= 10;
}
return n == sum_of_powers;
}
int main() {
int num;
std::cout << "Enter a number: ";
std::cin >> num;
if (is_armstrong_number(num)) {
std::cout << num << " is an Armstrong number." << std::endl;
} else {
std::cout << num << " is not an Armstrong number." << std::endl;
}
return 0;
}
//---------------------------------------------------------------------------------------
#include <iostream>
#include <cmath>
bool is_armstrong_number(int n) {
int num_digits = 0;
int temp = n;
while (temp > 0) {
temp /= 10;
num_digits++;
}
int sum_of_powers = 0;
temp = n;
while (temp > 0) {
int digit = temp % 10;
sum_of_powers += pow(digit, num_digits);
temp /= 10;
}
return n == sum_of_powers;
}
int main() {
std::cout << (is_armstrong_number(153) ? "True" : "False") << std::endl;
return 0;
}
// Output: True