C Program to Disply Armstrong Number Series

What is an Armstrong number?

An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits. This definition depends on the base b of the number system used, e.g., b = 10 for the decimal system or b = 2 for the binary system. (Read more about Armstrong number)

For generating Armstrong number series we use the following algorithm.

Algorithm

A positive integer is called an Armstrong number of order n if
abcd... = an + bn + cn + dn + ...
 
In case of 3 digit Armstrong numbers the sum of cubes of the digits of the number will be equal
to the actual number. Thus we can use a conditional statement to split a number, multiply, add
and check with original number.
 
#include < stdio .h >
#include < math .h >

int main()
{
int low, high, i, temp1, temp2, remainder, 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 by splitting the number
while (temp1 != 0)
{
temp1 /= 10;
++n;
}

// result contains sum of nth power of its digits. Here while loop is used
while (temp2 != 0)
{
remainder = temp2 % 10;
result += pow(remainder, 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;

}
return 0;
}

Comments

Popular posts from this blog

Algorithm to display "n" natural numbers #c

Algorithm to display the sum of n natural numbers #c

Algorithm for Sum of two numbers