Algorithm
1. Start
2. Read an integer 'num' from the user
3. Initialize variables: originalNum = num, sum = 0
4. Count the number of digits in 'num' and store it in a variable 'numDigits'
5. Repeat the following steps until num is not equal to 0:
a. Extract the last digit of 'num' and store it in a variable 'digit'
b. Compute 'digit' raised to the power of 'numDigits' and add it to 'sum'
c. Update 'num' by removing its last digit
6. Check if 'sum' is equal to 'originalNum'
a. If true, print "Armstrong number"
b. If false, print "Not an Armstrong number"
7. End
Code Examples
#1 Code Example- C++ program Check Armstrong Number of 3 Digits
Code -
C++ Programming
#include <iostream>
using namespace std;
int main() {
int num, originalNum, remainder, result = 0;
cout << "Enter a three-digit integer: ";
cin >> num;
originalNum = num;
while (originalNum != 0) {
// remainder contains the last digit
remainder = originalNum % 10;
result += remainder * remainder * remainder;
// removing last digit from the orignal number
originalNum /= 10;
}
if (result == num)
cout << num << " is an Armstrong number.";
else
cout << num << " is not an Armstrong number.";
return 0;
}
Copy The Code &
Try With Live Editor
Output
371 is an Armstrong number.
#2 Code Example- C++ program Check Armstrong Number of n Digits
Code -
C++ Programming
#include <cmath>
#include <iostream>
using namespace std;
int main() {
int num, originalNum, remainder, n = 0, result = 0, power;
cout << "Enter an integer: ";
cin >> num;
originalNum = num;
while (originalNum != 0) {
originalNum /= 10;
++n;
}
originalNum = num;
while (originalNum != 0) {
remainder = originalNum % 10;
// pow() returns a double value
// round() returns the equivalent int
power = round(pow(remainder, n));
result += power;
originalNum /= 10;
}
if (result == num)
cout << num << " is an Armstrong number.";
else
cout << num << " is not an Armstrong number.";
return 0;
}
Copy The Code &
Try With Live Editor
Output
1634 is an Armstrong number.
Demonstration
C++ Programing Example to Check Armstrong Number-DevsEnv