C++ Online Compiler
Example: Armstrong Number in C++ using Class and Object
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Armstrong Number in C++ using Class and Object #include <bits/stdc++.h> #include <cmath> using namespace std; class Armstrong { public: int p; float r = 0.00; // This method will check input number is Armstrong or not void CheckArmstrong(int x) { p = x; int q, rem, n = 0; // q - To store the storage of original input number to check // rem - To store the reminder // r - To store the result variable q = p; // store the number of digits of x in n for (q = p; q != 0; ++n) { q /= 10; } for (q = p; q != 0; q /= 10) { rem = q % 10; // store the sum of the power of individual digits in r r += pow(rem, n); } } // This method will print the final output of the program void ArmstrongNumber() { // if "p" is equal to "r", the number is an Armstrong number if ((int) r == p) cout << p << " is an Armstrong number.\n"; else cout << p << " is not an Armstrong number.\n"; } }; // It's the driver function int main() { int x; // To store the input number cout << "-----Enter the integer number-----\n"; cin >> x; // It will create the instance of the `Armstrong` class Armstrong AM; // It will produce the final output AM.CheckArmstrong(x); AM.ArmstrongNumber(); return 0; }
311
Output
Clear
ADVERTISEMENTS