# Armstrong Number in C

The Armstrong number is defined as the sum of each of its digits to the power of the number base for the each given number with any given number base. For example, for a three digit number like 370 the numbers 3, 7, 0 when raised to the power of three and then added together will give 370 i.e. 370 = (3*3*3) + (7*7*7) + (0*0*0).

## What is an Armstrong Number?

When the sum of a number’s digits raised to the power of the number of digits in the number equals the given number then it is referred to as an Armstrong number. There are a lot of Armstrong numbers.

Let’s take some examples of certain number base, for numbers with three digits there are only six Armstrong numbers in the range of 0-999 i.e. 0, 1, 153, 370, 371, 407 whereas for numbers with four digits there are three Armstrong numbers which are 1634, 8208, 9474 and there are many more Armstrong numbers with different number of digits.

XYZ = (x*x*x)+(y*y*y)+(z*z*z).

Let’s take an example from the above four digit Armstrong numbers and check if the number is an Armstrong number:

In the above example, we can see that when all the numbers of the digit 8208 are raised to the power of four (number base) are added together, they give 8208 (the number itself) as the result.

## Algorithm of Armstrong number

**The algorithm’s steps are given below:**

**Step 1:** Get user input.

**Step 2: **Set the temporary variable (var = num) to the initial value of sum, which is set to zero.

**Step 3: **Next, determine how many digits there are overall in the provided number.

**Step 4: **Then, the total amount of digits are stored in a variable (n).

**Step 5: **Continue the loop until var>0.

**Step 6: **The results of while loop are stored in sum.

**Step 7: **Then, we check if the user number is equal to sum or not.

**Step 8:** If it is equal to the user number then print, “It is an Armstrong number”.

**Step 9:** And, if it is not then print, “It is not an Armstrong number”.

## Flowchart to check a 3-digit Armstrong number:

## Program in C to check whether a number is a Armstrong number or not

```
#include <math.h>
#include <stdio.h>
int main() {
int n, Num, r, i = 0;
float result = 0.0;
printf("Enter an integer: ");
scanf("%d", &n);
Num = n;
for (Num = n; Num != 0; ++i) {
Num /= 10;
}
for (Num = n; Num != 0; Num /= 10) {
r = Num % 10;
result += pow(r, i);
}
if ((int)result == n)
printf("%d is an Armstrong number.", n);
else
printf("%d is not an Armstrong number.", n);
return 0;
}
```

**Output:**

```
1. Enter an integer: 370
370 is an Armstrong number.
2. Enter an integer: 372
372 is not an Armstrong number.
```

Therefore, with the help of the program given above, we can check if a given number is an Armstrong number or not. We can also make programs for specifically checking if a three digit number is an Armstrong number or not just by making some small changes to the algorithm and the code used for checking for an n digit number. When we don’t know the number of digits in a number before, just as explained in the algorithm in the program above, we follow all the steps to calculate the number of digits from the given user integer and store it.

In this program we initially calculate and store the integer's digit count in the variable i. Additionally, each time the second for loop iterates, the pow() (pow() function is stored in the math.h header file included above) function is used to calculate the power of each unique digit. After this is done then with the use of if statement we check if it is an Armstrong number or not by providing it with a condition.