TheAlgorithms-C-Plus-Plus/dynamic_programming/armstrong_number.cpp

22 lines
392 B
C++
Raw Normal View History

// Program to check whether a number is an armstrong number or not
#include <iostream>
using std::cout;
using std::cin;
int main() {
2019-08-21 10:10:08 +08:00
int n, k, d, s = 0;
cout << "Enter a number:";
cin >> n;
k = n;
while (k != 0) {
2019-08-21 10:10:08 +08:00
d = k % 10;
s += d * d * d;
2019-08-21 10:10:08 +08:00
k /= 10;
2017-03-30 21:43:26 +08:00
}
2019-08-21 10:10:08 +08:00
if (s == n)
cout << n << "is an armstrong number";
2017-03-30 21:43:26 +08:00
else
2019-08-21 10:10:08 +08:00
cout << n << "is not an armstrong number";
2017-03-30 21:43:26 +08:00
}