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. Logic to print Armstrong numbers in given range in C programming.

Example

Input

Enter lower limit: 1
Enter upper limit: 1000

Output

Armstrong number between 1 to 1000 are:
1, 2, 3, 4, 5, 6, 7, 8, 9, 370, 371, 407

Required knowledge

Basic C programming, If statement, For loop, While loop, Nested loop

Must know –

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 = 61 = 6
371 = 33 + 73 + 13 = 371

Logic to find all Armstrong number between 1 to n

Step by step descriptive logic to generate Armstrong numbers:

  1. Input upper limit to print Armstrong number from user. Store it in some variable say end.
  2. Run a loop from 1 to end, increment 1 in each iteration. The loop structure should look like for(i=1; i<=end; i++).
  3. Inside the loop print current number i, if it is Armstrong number.

Program to find all Armstrong numbers from 1 to n


/**
 * C program to print Armstrong numbers from 1 to n
 */
#include <stdio.h>
#include <math.h>

int main()
{
    int num, lastDigit, digits, sum, i, end;

    /* Input upper limit from user */
    printf("Enter upper limit: ");
    scanf("%d", &end);

    printf("Armstrong number between 1 to %d are: \n", end);

    for(i=1; i<=end; i++)
    {
        sum = 0;

        /* Copy the value of num for processing */
        num = i;

        /* Find total digits in num */
        digits = (int) log10(num) + 1;

        /* Calculate sum of power of digits */
        while(num > 0)
        {
            /* Extract last digit */
            lastDigit = num % 10;

            /* Find sum of power of digits */
            sum = sum + pow(lastDigit, digits);

            /* Remove the last digit */
            num = num / 10;
        }

        /* Check for Armstrong number */
        if(i == sum)
        {
            printf("%d, ", i);
        }

    }

    return 0;
}

Once, you are done with generating Armstrong numbers from 1 to n. You can easily modify the logic to work it for given ranges. Below program generates Armstrong numbers in a given range.

Program to find Armstrong numbers in given range

/**
 * C program to generate Armstrong numbers in a given range
 */
#include <stdio.h>
#include <math.h>

int main()
{
    int num, 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 num for processing */
        num = i;

        /* Find total digits in num */
        digits = (int) log10(num) + 1;

        /* Calculate sum of power of digits */
        while(num > 0)
        {
            /* Extract the last digit */
            lastDigit = num % 10;

            /* Find sum of power of digits */
            sum = sum + pow(lastDigit, digits);

            /* Remove the last digit */
            num = num / 10;
        }

        /* Check for Armstrong number */
        if(i == sum)
        {
            printf("%d, ", i);
        }
    }

    return 0;
}

Output

Enter lower limit: 1
Enter upper limit: 10000
Armstrong number between 1 to 10000 are:
1, 2, 3, 4, 5, 6, 7, 8, 9, 370, 371, 407, 1634, 8208, 9474,

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>