Problem: Armstrong Number Check
Write a C++ program to check if a given number is an Armstrong number or not. An Armstrong number is a number that is equal to the sum of its own digits raised to the power of the number of digits.
Your program should take an input number from the user and output whether it is an Armstrong number or not.
Example:
Enter a number: 153
153 is an Armstrong number.
Enter a number: 371
371 is an Armstrong number.
Enter a number: 123
123 is not an Armstrong number.
Hint: To check if a number is an Armstrong number, you can calculate the sum of the digits raised to the power of the number of digits. You can convert the number to a string to count the number of digits and then iterate over the digits to calculate the sum.
You can start by setting up the basic structure of the program and then gradually complete it. Let me know if you need further assistance!
#include <iostream>
#include <cmath>
using namespace std;
int main() {
string armNum;
int total = 0;
cout << "Enter a number: ";
cin >> armNum;
for (int i = 0; i < armNum.length(); i++) {
int integer = int(armNum[i]) - int('0');
int power = pow(integer, armNum.length());
total += power;
}
string strTotal = to_string(total);
if(strTotal == armNum){
cout << armNum << " is an Armstrong Number";
}else{
cout << armNum << " is not an Armstrong Number";
}
return 0;
}