C Programming Code Examples C > For Loops and While Loops Code Examples C program to find Armstrong numbers between 1 to n C program to find Armstrong numbers between 1 to n Write a C program to print all Armstrong numbers between 1 to n. How to print Armstrong numbers between given interval using loop in C program. What is Armstrong number? An Armstrong number is a n-digit number that is equal to the sum of nth power of its digits. For example, 6 = 6^1 = 6 407 = 4^3 + 0^3 + 7^3 = 407 Logic to find all Armstrong number between 1 to n Input upper limit to print Armstrong number from user. Store it in some variable say end. Run a loop from 1 to end, increment 1 in each iteration. The loop structure should look like for(x=1; x<=end; x++). Inside the loop print current number x, if it is Armstrong number. #include <stdio.h> #include <math.h> int main() { int j, lastDigit, digits, sum, x, end; /* Input upper limit from user */ printf("Enter upper limit: "); scanf("%d", &end); printf("Armstrong number between 1 to %d are: \n", end); for(x=1; x<=end; x++) { sum = 0; /* Copy the value of j for processing */ j = x; /* Find total digits in j */ digits = (int) log10(j) + 1; /* Calculate sum of power of digits */ while(j > 0) { /* Extract last digit */ lastDigit = j % 10; /* Find sum of power of digits */ sum = sum + pow(lastDigit, digits); /* Remove the last digit */ j = j / 10; } /* Check for Armstrong number */ if(x == sum) { printf("%d, ", x); } } return 0; }