Advertisement
albela

Untitled

Apr 18th, 2021
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. // Q2.Write a program to check Armstrong no.
  2. //Solution:-
  3.  
  4. #include <cmath>
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. int main() {
  10.    int num, originalNum, remainder, n = 0, result = 0, power;
  11.    cout << "Enter an integer: ";
  12.    cin >> num;
  13.  
  14.    originalNum = num;
  15.  
  16.    while (originalNum != 0) {
  17.       originalNum /= 10;
  18.       ++n;
  19.    }
  20.    originalNum = num;
  21.  
  22.    while (originalNum != 0) {
  23.       remainder = originalNum % 10;
  24.  
  25.       // pow() returns a double value
  26.       // round() returns the equivalent int
  27.       power = round(pow(remainder, n));
  28.       result += power;
  29.       originalNum /= 10;
  30.    }
  31.  
  32.    if (result == num)
  33.       cout << num << " is an Armstrong number.";
  34.    else
  35.       cout << num << " is not an Armstrong number.";
  36.    return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement