Python Examples

Python Program to Check Armstrong Number

An armstrong number in python is a positive integer of base n, and the sum of each digit of the Armstrong number of order n is equal to the number itself. 

Python Program to check Armstrong Number

For example, let us consider ABC of order ‘n’ then ABC is an Armstrong number if:

ABC= An+Bn+Cn

Let us consider an Armstrong number 371, the sum of the cubes of each digit of 371 is equal to the number itself. For example: 

371= 3**3 + 7**3+ 1**3

Similarly if we consider 9474

9474= (9x9x9x9)+ (4x4x4x4)+ (7x7x7x7)+ (4x4x4x4)

=6561+256+2401+ 256

= 9474

Hence when we take the sum of the digits to the power of 4 and add them we get the original number, therefore it is said to be an Armstrong number.

Source Code to check Armstrong number program in python (For three digits):

                    

n = int(input("Enter a number: "))



#initialize the sum

s = 0



t = n

while t > 0:

   digit = t % 10

   s += digit ** 3

   t //= 10



# display the result

if n == s:

   print(n,"is an Armstrong number")

else:

   print(n,"is not an Armstrong number")



Output:

Enter a number: 153

153 is an Armstrong number

In the above program, the sum of the cube of each digit as we considered a three digit number. Firstly, we initialize the value of sum ‘s’ as 0. Using the % operator we obtain each digit of the number. When the remainder of the number is then divided by 10, we achieve the last digit of the original number. After this step we take the cubes of the digits using the exponent operator. 

Lastly, if the sum is equal to the original number, we can say that the number is an Armstrong number. Whereas, if the sum is not equal to the original number, the number is not an Armstrong number. 

What is the Armstrong 4 digit number?

armstrong number in python

Source

A four-digit armstrong number python is a number when the sum of each individual digit raised to the power of 4 is equal to the number itself. 

How do I find my 4 digit Armstrong number?

The process of finding out whether the 4-digit number is an Armstrong number or not is pretty simple. 

The process involves raising each individual digit to the power of 4 and adding the sum of all the digits together. If the sum is equal to the original number, then it is considered an Armstrong number else, it is not an Armstrong number.  Some 4-digit Armstrong numbers include 1634, 8208, and 9474. 

Algorithm to check if the given number is Armstrong or not

To check if two numbers are armstrong number python or not, we need two parameters. The first parameter will be for the number of digits, and the second parameter will be for the sum of the individual digits raised to the power of n. 

Step 1: Start

Step 2: Initialize the variables n, s, temp.

Step 3: Get the input of n from the user.

Step 4: Set the values of s=0 and temp=n

Step 5: Repeat the steps given below while num>=0

5.1: S= s+sum of the cube of the last digits

5.2: n=n/10

Step 6: if s==temp

Print “This is an Armstong number.”

Else

Print “This is not an Armstong number.”

Step 7: Stop;

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;



   // store the number of digits of numb in n

   for (oriNum = numb; oriNum != 0; ++n) {

       oriNum /= 10;

   }



   for (oriNum = numb; oriNum != 0; oriNum /= 10) {

       remainder = oriNum % 10;



      // store the sum of the power of individual digits in the result

      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: 8208

8208 is an Armstrong number.

Is 371 an Armstrong number?

We have previously learned that an armstrong number python is a number whose that is equal to the sum of the power of its digits. 

To check if 371 is an Armstrong number or not, we will follow the steps given below:

Considering 371 to be an Armstrong number we will multiple the cube of each individual digit and then add the sum of each digit. 

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. 

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.