C++ Program to Print First n Armstrong Numbers using While Loop
Write C++ Program to Print First n Armstrong Numbers using While Loop
// CPP Program to Print First n Armstrong Numbers using While 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)); while(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; cout << "Enter the number ( < 6) :--> "; cin >> n; long int i = 1 ; int Count = 0; cout << "First " << n << " Armstrong Numbers Are :--> "; while(Count < n) { if(isArmstrong(i)) { cout << i << " "; Count++; } i++; } return 0; }
Output:
Enter the number ( < 6) :--> 5
First 5 Armstrong Numbers Are :--> 1 153 370 371 407