C program to find the Armstrong numbers in entered range.

 C program to find the Armstrong numbers in entered range.
     
      Using this program user can find Armstrong numbers between entered two intervals.If sum of cubes of each digit of 3 digit number is equal to entered number, then the number is called an Armstrong number.


Program:

#include <stdio.h>
#include <math.h>
/* To find the Armstrong number between two intervals by SlashMyCode.com */
int main()
{
    int low, high, i, temp1, temp2, rem, n=0, result=0;

    printf("Enter two numbers(intervals): ");
    scanf("%d %d", &low, &high);
    printf("Armstrong numbers between %d an %d are: ", low, high);

    for(i = low + 1; i < high; ++i)
    {
        temp2 = i;
        temp1 = i;

        // number of digits calculation
        while (temp1 != 0)
        {
            temp1 /= 10;
            ++n;
        }

        // result contains sum of nth power of its digits
        while (temp2 != 0)
        {
            rem = temp2 % 10;
            result += pow(rem, n);
            temp2 /= 10;
        }

        // checks if number i is equal to the sum of nth power of its digits
        if (result == i) {
            printf("%d ", i);
        }

        // resetting the values to check Armstrong number for next iteration
        n = 0;
        result = 0;
/* To find the Armstrong number between two intervals by SlashMyCode.com */
    }
    return 0;

}


Output:

Output of C program to find the Armstrong numbers in entered range.




No comments:

Post a Comment

Feel free to ask us any question regarding this post