Skip to content

Commit 04a2a53

Browse files
Create ArmstrongNumber.cpp
1 parent 009eb15 commit 04a2a53

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
bool isArmstrongNumber(int num) {
8+
if (num < 0) {
9+
return false;
10+
}
11+
12+
int originalNum = num;
13+
int numberOfDigits = 0;
14+
while (originalNum != 0) {
15+
numberOfDigits++;
16+
originalNum /= 10;
17+
}
18+
19+
int sumOfPowers = 0;
20+
originalNum = num;
21+
while (originalNum != 0) {
22+
int digit = originalNum % 10;
23+
sumOfPowers += pow(digit, numberOfDigits);
24+
originalNum /= 10;
25+
}
26+
27+
return (sumOfPowers == num);
28+
}
29+
};
30+
31+
int main() {
32+
int number;
33+
cout << "Enter a number: ";
34+
cin >> number;
35+
36+
Solution s;
37+
bool isArmstrong = s.isArmstrongNumber(number);
38+
39+
if (isArmstrong) {
40+
cout << number << " is an Armstrong number." << endl;
41+
} else {
42+
cout << number << " is not an Armstrong number." << endl;
43+
}
44+
45+
return 0;
46+
}

0 commit comments

Comments
 (0)