0% found this document useful (0 votes)
20 views

C++ Armstrong Number

The document contains C++ code to check if a given integer is an Armstrong number. It takes the input number, separates each digit, raises it to the power of the number of digits, and sums the results. If the sum equals the original number, it is an Armstrong number, otherwise it is not. The code uses loops and mathematical functions like pow() and round() to perform the steps.

Uploaded by

Animal Cares
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

C++ Armstrong Number

The document contains C++ code to check if a given integer is an Armstrong number. It takes the input number, separates each digit, raises it to the power of the number of digits, and sums the results. If the sum equals the original number, it is an Armstrong number, otherwise it is not. The code uses loops and mathematical functions like pow() and round() to perform the steps.

Uploaded by

Animal Cares
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#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;
}

#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;
}

You might also like