C++ Program to Print All Armstrong Numbers Between n and m using While Loop
Write C++ Program to Print All Armstrong Numbers Between n and m using While Loop
// CPP Program to Print All Armstrong Numbers Between n and m using While Loop #include <iostream> #include <stdbool.h> using namespace std; bool isArmstrong(int n) { int temp = n, ans = 0; while(n != 0) { int remainder = n % 10; ans = ans + (remainder * remainder * remainder); n = n / 10; } if(temp != ans) { return false; } return true; } int main() { int n,m; cout << "Enter n :--> "; cin >> n; cout << "Enter m :--> "; cin >> m; cout << "Armstrong numbers between " << n << " to " << m << " :--> "; int i = n ,j = m; while(i < j) { if(isArmstrong(i)) { cout << i << " "; } i++; } return 0; }
Output:
Enter n :--> 300
Enter m :--> 400
Armstrong numbers between 300 to 400 :--> 370 371