Introduction
Armstrong number is the number in any given number base, which forms the total of the same number, when each of its digits is raised to the power of the number of digits in the number. It is of special interest to new programmers and those learning a new programming language because of the way the number behaves in a given number base.
For example, using a simple number 153 and the decimal system, we see there are 3 digits in it. If we do a simple mathematical operation of raising each of its digits to the power of 3, and then totalling the sum obtained, we get 153. That is 1 to the power of 3 5 to the power of 3 3 to the power of three is 1 125 27 153. This can also be represented as 1^3 5^3 3^3=153. The number 153 is anย example of the Armstrong numberย which also has a unique property that one can use any number system.
Thus if the number obtained totals to or equals the original number when each of the digits is raised to the power of the number of digits in the number and added to obtain a number, in any given number system, such a number is called anย Armstrong number.
To understand the logical formula of Armstrong numbers, one needs to remember that the Armstrong number property is true in any number system. Let us take the number 548834 with 6 digits and see if it satisfies the property of Armstrong numbers. Use the equation
ย 5^6 4^6 8^6 8^6 3^6 4^6 =? Calculate the sum obtained by raising each of the digits to the power of six and adding together the sum of the terms obtained. The total is 548834 which is the original number itself. Thus 548834 is an Armstrong number since one gets the same number when one adds the individual terms of the digits in the number raised to the power of the number of digits in the number.ย
Let us look at what is Armstrong numberย by taking the number 122. In base 3 to check if 122 is an Armstrong no, perform the operation 1^3 2^3 2^3=17. In base 3 the operation is the same as 2*1 2*3 1*9=17. Important to note here is that 3 to the power of zero is equal to 1, and 3 to the power of 1 is 3, and 3 to the power of two is 9. Totalling the terms obtained we get 17. This means that an Armstrong number can exhibit the same property in any given number system.
0, 1, 153, 370, 371 and 407 are anย Armstrong number listย or all examples of Armstrong numbers. Let us check why?
ย For 0, the operation is 0^1=0
ย For 1, the operation is 1^1=1
ย For 153, the operation is 1^3 5^3 3^3=153
ย For 370 the operation is 3^3 7^3 0^3=370
ย For 371 the operation is 3^3 7^3 1^3=371
ย For 407 the operation is 4^3 0^3 7^3=407
Let us now look at what is Armstrong number behavior with a 4 digit Armstrong number. The operation involves raising each of the digits to the power of 4 and then totaling the terms obtained. Armstrong numbers with 4 digits are 1634, 8208, and 9474 among others. Armstrong numbers between 1 to 10000 can easily be found following these rules.
There are two parameters to implement and verify the property of what is Armstrong number by anย Armstrong number algorithm.ย The first parameter inย Armstrong number logicย is the numberโs number of digits and the second parameter is the sum of the terms when each of its digits is raised to the power of the number of digits in the number. Take a look at the Armstrong number algorithm to obtain a better understanding:
Here are the 8 steps involved in aย program for Armstrong number.
1. The number of digits in num is determined and found out.
ย 2. Theย sum of digits of a number in Pythonย or individual digit sums are got by performing num mod 10, where mod is called the remainder operation.
ย 3. The individual digit is then raised to the power (number of digits) and stored.
ย 4. The number is then divided by 10 in order to obtain the second digit.
ย 5. All the above 3-steps numbered Steps 2, 3 and 4 are performed until the value of num is greater than 0.
ย 6. When the num is less than 0, end the while loop.
ย 7. Check the sum obtained orย Armstrong valueย is the same as the original number
ย 8. When yes, the number is labelled an Armstrong number
Now use Python to understandย what is Armstrong numberย by implementing the above algorithm in Python as below to understandย what is Armstrong number in python.
num= int(input(‘Enter a number: ‘))
num_original =num2=num
sum1 = 0
cnt=0
while(num>0):
ย ย ย ย ย ย ย ย ย ย ย ย ย ย cnt=cnt 1
ย ย ย ย ย ย ย ย ย ย ย ย ย ย num=num//10
while num2>0:
ย ย ย rem = num2% 10
ย ย ย sum1ย = rem ** cnt
ย ย ย num2//= 10
if(num_original==sum1):
ย ย ย ย print(‘Armstrong!!’)
else:
ย ย ย ย print(‘Not Armstrong!’)
Output
ย Now in the field โEnter a number, enter theย Armstrong number in pythonย or number: 153. The output showsย Armstrong number Pythonย โArmstrongโ.
ย Next try the algorithm with the field โEnter a number as 134 or Enter a number: 134. The output this time is, Not Armstrong!
To test the code: Rememberย num stores the number of digits in the input whereas num_original stores the initial value of the input.ย 1 Armstrong is equal toย sum 1=0 and cnt=0. Using the variable num_2, one computes the sum of individual digits in the original number raised to the power of the count variable. Then compare the original number with the sum obtained to see if it exhibits properties in theย Armstrong number program in Pythonย to be an Armstrong number
This program generates an Armstrong number list between 0 and 999. An Armstrong number is one whose sum of digits raised to the power three equals the number itself. 371, for example, is an Armstrong number because 3**3 + 7**3 + 1**3 = 371.
PROGRAMย ArmstrongNumber
ย ย ย IMPLICITย NONE
ย ย ย INTEGER :: a, b, c ย ย ย ย ย ย ย ย ย โthe three digitsโ
ย ย ย INTEGER :: abc, a3b3c3 ย ย ย ย ย ย ย โthe number and its cubic sumโ
ย ย ย INTEGER :: Count ย ย ย ย ย ย ย ย ย ย โa counterโ
ย ย ย Count = 0
ย ย ย DO a = 0, 9ย ย ย ย ย ย ย ย ย ย ย ย ย โfor the left most digitโ
ย ย ย ย ย ย DO b = 0, 9 ย ย ย ย ย ย ย ย ย ย ย โfor the middle digitโ
ย ย ย ย ย ย ย ย ย DO c = 0, 9 ย ย ย ย ย ย ย ย ย ย ย ย โfor the right most digitโ
ย ย ย ย ย ย ย ย ย ย ย ย abcย ย = a*100 + b*10 + c ย !ย ย ย ย โthe numberโ
ย ย ย ย ย ย ย ย ย ย ย ย a3b3c3 = a**3 + b**3 + c**3 !ย ย ย ย โthe sum of cubesโ
ย ย ย ย ย ย ย ย ย ย ย ย IF (abc == a3b3c3) THEN ย ย !ย ย ย ย โif they are equalโ
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Count = Count + 1ย ย ย ย ! ย ย ย ย ย โcount and display itโ
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย WRITE(*,*)ย ‘Armstrong number ‘, Count, ‘: ‘, abc
ย ย ย ย ย ย ย ย ย ย ย ย END IF
ย ย ย ย ย ย ย ย ย END DO
ย ย ย ย ย ย END DO
ย ย ย END DO
END PROGRAMย ArmstrongNumber
The output of the preceding program is shown below. There are six Armstrong numbers between 0 and 999.
Armstrong number 1: 0
Armstrong number 2: 1
Armstrong number 3: 153
Armstrong number 4: 370
Armstrong number 5: 371
Armstrong number 6: 407
One can study what is Armstrong number, its implementation and its algorithm in Python here. So, are there any practical applications using the unique property of Armstrong number? In reality, there are none and the uniqueness of these numbers hold no practical use except as examples or learning tools to verify programs, learn concepts better and explore the rules in a new programming language.
If you are interested in making a career in the Data Science domain, our 11-month in-personย Post Graduate Certificate Program in Data Science & Machine Learning course can help you immensely in becoming a successful Data Science professional.ย
Fill in the details to know more
From The Eyes Of Emerging Technologies: IPL Through The Ages
April 29, 2023
Data Visualization Best Practices
March 23, 2023
What Are Distribution Plots in Python?
March 20, 2023
What Are DDL Commands in SQL?
March 10, 2023
Best TCS Data Analyst Interview Questions and Answers for 2023
March 7, 2023
Best Data Science Companies for Data Scientists !
February 26, 2023
Add your details:
By proceeding, you agree to our privacy policy and also agree to receive information from UNext through WhatsApp & other means of communication.
Upgrade your inbox with our curated newletters once every month. We appreciate your support and will make sure to keep your subscription worthwhile