To find an Armstrong number python in an interval, we can take the help of two Python concepts, include:
- if-else statements
- The while loop
An Armstrong number is a positive integer that has a base of n such that:
PQR= P^n+Q^n+R^n
From the above equation, we can derive that an Armstrong number is a number whose sum of individual digits to the power of n is equal to the original number itself.Â
Source Code
lwr = 100
uppr = 2000
for numb in range(lwr, uppe + 1):
   # order of number
   odr = len(str(numb))
   Â
   # initialize sum
   sum = 0
   tem = numb
   while tem > 0:
       digit = tem % 10
       sum += digit ** odr
       tem //= 10
   if numb == sum:
       print("The Armstrong numbers are: ",numb)
Output
The Armstrong numbers are:
153
370
371
407
1634
What is an Armstrong number in Python?
Armstrong number python is the sum of each digit of the number of order ‘n’ that equals the number itself.
Let us take some examples:
Find out whether 123 is an Armstrong number or not?
Ans: For 123 to be an Armstrong number, the sum of the cubes of each digit of 123 is equal to the number itself.
For example:Â
123= 1x1x1+2x2x2+3x3x3
=1+8+27
=36
123 is not equal to 36, hence 123 is not an Armstrong number.Â
Similarly, if we consider 8208
 8208= (8x8x8x8)+ (2x2x2x2)+ (0x0x0x0)+ (8x8x8x8)
 = 4096+ 16+0+4096
 = 8208
What is the Armstrong 4 digit number?
When a sum of the individual digits of a number is raised to the power of 4 and the sum is equal to the original number itself, such a number is known as an Armstrong number.
 How do I find my 4 digit Armstrong number?
A 4-digit number is called an Armstrong number if the sum of the digits raised to the power of 4 is equal to the original number itself.Â
For example:Â
8208 is an Armstrong number because the sum of the individual digits raised to the power of 4 is equal to the number itself.Â
8208= (8x8x8x8)+ (2x2x2x2)+ (0x0x0x0)+ (8x8x8x8)
 = 4096+ 16+0+4096
 = 8208
Is 371 an Armstrong number?
We have learned that an Armstrong number python is a number that is equal to the sum of the power of its digits or order n.Â
 To check if 371 is an Armstrong number or not, we will have to check whether 371 is equal to the sum of the cube of its digits:
 For example:Â
 371 = (3x3x3)+(7x7x7)+(1x1x1) Â
where:Â Â
 3 cube=27 Â
7 cube=343Â Â
1 cube=1Â Â
 Now we add the sum of all the cubes together:
 27+343+1=371 Â
 Therefore 371 is an Armstrong number.Â
Source code to check Armstrong Number of n digits
#include
#include
Â
int main() {
   int numb, oriNum, rem, n = 0;
   float result = 0.0;
Â
   printf("Enter an integer: ");
   scanf("%d", &num);
Â
   oriNum = numb;
Â
   // number of digits of numb is saved in n
   for (oriNum = numb; oriNum != 0; ++n) {
       oriNum /= 10;
   }
Â
   for (oriNum = numb; oriNum != 0; oriNum /= 10) {
       remainder = oriNum % 10;
Â
      result += pow(remainder, n);
   }
Â
   if ((int)result == numb)
    printf("%d is an Armstrong number.", numb);
   else
    printf("%d is not equal to the original number, hence it is not an Armstrong number", numb);
   return 0;
}
 Output
Enter an integer: 371
371 is an Armstrong number.
Leave a Reply