Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Q2.Write a program to check Armstrong no.
- //Solution:-
- #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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement