C++ Online Compiler
Example: Armstrong Number in C++ using Function
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Armstrong Number in C++ using Function #include <bits/stdc++.h> #include <cmath> using namespace std; // This function will check input number is Armstrong or not void CheckArmstrong(int p) { int q, rem, n = 0; float r = 0.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); } // 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 produce the final output CheckArmstrong(x); return 0; }
407
Output
Clear
ADVERTISEMENTS