C++ Online Compiler
Example: Armstrong Number in C++ using For loop
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Armstrong Number in C++ using For loop #include <iostream> #include <cmath> using namespace std; int main() { int x, y, rem, n = 0; float r = 0.0; // x - To store the input number // y - To store the storage of original input number to check // rem - To store the reminder // r - To store the result variable cout << "-----Enter the integer number-----\n"; cin >> x; y = x; // store the number of digits of x in n for (y = x; y != 0; ++n) { y /= 10; } for (y = x; y != 0; y /= 10) { rem = y % 10; // store the sum of the power of individual digits in r r += pow(rem, n); } // if x is equal to r, the number is an Armstrong number if ((int) r == x) { cout << x << " is an Armstrong number.\n"; } else { cout << x << " is not an Armstrong number.\n"; } return 0; }
407
Output
Clear
ADVERTISEMENTS