Print all Armstrong numbers between 1 to n with C
#include <stdio.h>
#include <math.h>
int main()
{
int num, lastDigit, digits, sum, i, last;
printf("Enter upper limit: ");
scanf("%d", &last);
printf("Armstrong number between 1 to %d are: \n", last);
for (i = 1; i <= last; i++)
{
sum = 0;
num = i;
digits = (int)log10(num) + 1;
while (num > 0)
{
/* Extract last digit */
lastDigit = num % 10;
sum = sum + ceil(pow(lastDigit, digits));
num = num / 10;
}
if (i == sum)
{
printf("%d, ", i);
}
}
return 0;
}
Output:-
Enter upper limit : 50
Armstrong number between 1 to 50 are :
1 2 3 4 5 6 7 8 9
0 Comments