C program to find Armstrong numbers between 1 to n

Previous Program Next Program

Write a C program to enter any number and print all Armstrong numbers between 1 to n. How to print Armstrong numbers between given interval using loop in C program. Logic to generate Armstrong numbers in given range in C program.

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, For loop, Nested loop, If else

What is Armstrong number?

An Armstrong number is an n-digit number that is equal to the sum of the nth powers of its digits. Read more about Armstrong numbers. Below are few examples of Armstrong numbers:
6 = 61 = 6
371 = 33 + 73 + 13 = 371

Logic to generate Armstrong number from 1 to n

Below is the step by step descriptive logic to generate Armstrong numbers:

  1. Read upper limit to generate Armstrong number from user. Store it in some variable say end.
  2. Run a loop from 1 to end, incrementing 1 in each iteration. The loop structure should look like for(i=1; i<=end; i++). This loop will iterate through n numbers. Inside this loop I will check each number for Armstrong number.
  3. Inside the loop print the current number if it is Armstrong number. Read the below post for details about checking Armstrong number.

Program to generate Armstrong numbers from 1 to n

#include <stdio.h>
#include <math.h>

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

    /*
     * Read 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 the last digit
            lastDigit = num % 10;

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

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

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

    }

    return 0;
}

So whats next now? 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

#include <stdio.h>
#include <math.h>

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

    /*
     * Read 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
            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 ;)

Recommended post

Previous Program Next Program
Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)

No comments:

Post a Comment