C++ Program to Print Armstrong Number Between 1 to n using For Loop
Write C++ Program to Print Armstrong Number Between 1 to n using For Loop
// CPP Program to Print Armstrong Number Between 1 to n using For Loop #include <iostream> #include <stdbool.h> #include <math.h> using namespace std; bool isArmstrong(int n) { int temp = n,ans = 0, rem; int digits = ceil(log10(n)); for(; n != 0; ) { int remainder = n % 10; ans = ans + (remainder * remainder * remainder); n = n / 10; } if(temp == ans) { return true; } return false; } int main() { int n, i; cout << "Enter Number :--> "; cin >> n; cout << "Armstrong Numbers Between 1 to " << n << " :--> "; for(i = 1; i < n; i++) { if(isArmstrong(i)) { cout << i << " "; } } return 0; }
Output:
Enter the number :--> 1000
Armstrong numbers between 1 to 1000 :--> 1 153 370 371 407