Python Examples

Python Program to Find Armstrong Number in an Interval

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.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.