Updated April 15, 2023

## Introduction to Armstrong Number in C

Armstrong Number is any number that equals The sum of the cube of its digit. The sum of the cube of its all digit equals that number is an Armstrong Number. This article will discuss how to check the number using the C programming language, whether it is an Armstrong number or not. Armstrong numbers are – 0, 1, 153, and 407. Mathematical computation for checking an Armstrong number is as follows:

- 0 = 0 * 0 * 0 = 0
- 1 = 1 * 1 * 1= 1
- 153 = (1 * 1 * 1) + (5 * 5 * 5) + (3 * 3 * 3) = 1 + 125 + 27 = 153
- 407 = (4 * 4 * 4) + (0 * 0 * 0) + (7 * 7 * 7) = 64 + 0 + 343 = 407

### Algorithm to check Armstrong Number

**Step 1:** Enter any Number

**Step 2:** Find the cube of each digit of the entered number.

**Step 3:** Then, add the cube of all the digits.

**Step 4:** If the result of step 3 is equal to the entered number, i.e., Step 1. Then the print entered number is an Armstrong number.

**Step 5:** If the result of step 3 is equal to the entered number, i.e., Step 1. Then print entered number is not an Armstrong number.

### Examples of Armstrong Number

This section will discuss how to check Armstrong’s number using various methods.

#### Example #1

Program to Check Armstrong Number using while loop

```
#include<stdio.h>
#include<conio.h>
int main()
{
int num, r, sum=0, temp_num;
printf("Enter number to check Armstrong number ");
scanf("%d", &num);
temp_num = num;
while(num > 0)
{
r = num % 10;
sum = sum + (r * r * r);
num = num / 10;
}
if(temp_num == sum)
{
printf("Entered number is Armstrong Number.");
printf("\n");
}
else
{
printf("Entered number is not Armstrong Number.");
printf("\n");
}
return 0;
}
```

**Output:**

**Explanation:**

This is a program to check Armstrong’s number using a while loop. In this program, first, it asks a user to give input. Then the entered number is copied into temp_num. Here temp_num will compare the final result with the input value. While condition checks if the number is greater than 0 or not. If the number exceeds 0, it executes the statements afterward. First, the last digit is separated from num by performing num%10. Then the digit is cubed, and the sum is stored. Then the last digit is discarded using num/10. This process will be performed on all digits in the number. Then temp_num and num are compared; if the input value and final results are equal, it will print that Entered number is an Armstrong Number. If both are not equal, it will print Entered number, not Armstrong’s.

#### Example #2

Program to Check Armstrong Number using do-while loop

```
#include<stdio.h>
#include<conio.h>
int main()
{
int num, r, sum=0, temp_num;
printf("Enter number to check Armstrong number ");
scanf("%d", &num);
temp_num = num;
do
{
r = num % 10;
sum = sum + (r * r * r);
num = num / 10;
} while(num > 0);
if(temp_num == sum)
{
printf("Entered number is Armstrong Number.");
printf("\n");
}
else
{
printf("Entered number is not Armstrong Number.");
printf("\n");
}
return 0;
}
```

**Output:**

**Explanation:**

This program checks whether the input number is an Armstrong number or not using the do-while loop. The working of this program is the same as above example 1. However, in the above example, the only difference is that it first tests the condition, i.e., number > 0. And here, this example verifies the same condition at the end of the loop.

#### Example #3

Print Armstrong Number using for loop

```
#include<stdio.h>
#include<conio.h>
int main()
{
int lower_limit, upper_limit, i, r, sum, temp_num;
printf("Enter lower limit ");
scanf("%d", &lower_limit);
printf("Enter uppee limit ");
scanf("%d", &upper_limit);
printf("\nList of Armstrong numbers between %d an %d are: ", lower_limit, upper_limit);
for(i = lower_limit; i <= upper_limit; i++)
{
sum = 0;
temp_num = i;
for(; temp_num >0; temp_num /= 10)
{
r = temp_num % 10;
sum = sum + (r * r * r);
}
if(sum == i)
printf("\n %d", i);
}
return 0;
}
```

**Output:**

**Explanation:**

In this program, the user will enter two numbers, and the program will check for an Armstrong number and print it. The lower limit takes the minimum number, and the upper limit takes the maximum number. If the number of the upper limit is small, the lower limit will raise an error. Therefore, the number of the upper limit should be greater than the lower. Each number is stored in temp num between the time interval. Then in variable r, each digit of the number is retrieved, and then the cube is found. The product is then the cube.

### Conclusion

In this article, we have seen Armstrong’s number and various programs to check whether the number is Armstrong or not using various loops in C.

### Recommended Articles

This is a guide to Armstrong Number in C. Here we discuss the introduction, the Algorithm to check the Armstrong number, and the Examples of the Armstrong Number in C along with outputs. You can also go through our other suggested articles to learn more –