 # C Programming Code Examples

## C > For Loops and While Loops Code Examples

### Program to find Armstrong numbers in given range

``` Program to find Armstrong numbers in given range 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, 371 = 3^3 + 7^3 + 1^3 = 371 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(i=1; i<=end; i++). Inside the loop print current number i, if it is Armstrong number. #include <stdio.h> #include <math.h> int main() { int j, lastDigit, digits, sum, i; int start, end; /* Input lower and upper limit from user */ printf("Enter lower limit: "); scanf("%d", &start); printf("Enter upper limit: "); scanf("%d", &end); printf("Armstrong number between %d to %d are: \n", start, end); for(i=start; i<=end; i++) { sum = 0; /* Copy the value of j for processing */ j = i; /* Find total digits in j */ digits = (int) log10(j) + 1; /* Calculate sum of power of digits */ while(j > 0) { /* Extract the 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(i == sum) { printf("%d, ", i); } } return 0; } ```