C Program to Print First n Armstrong Number using For loop
Write a Program to Print First n Armstrong Number using For loop
// C Program to Print First n Armstrong Number using For loop #include <stdio.h> #include <stdbool.h> #include <math.h> 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; printf("Enter the number ( < 6) :--> "); scanf("%d", &n); long int i = 1 ; int count = 0; printf("First %d Armstrong Numbers Are :--> ", n); for(; count < n; ) { if(isArmstrong(i)) { printf("%ld ",i); count++; } i++; } return 0; }
Output:
Enter the number ( < 6) :--> 5
First 5 Armstrong Numbers Are :--> 1 153 370 371 407