Table of Contents
What is an Armstrong Number?
According to mathematical number theory, the Armstrong number is any number that, when each of its digits is raised to the power of the number of digits in the number, generates the sum of the same number in every given number base. Using the decimal system and the basic number 153 as an example, we can observe that it has 3 digits. By performing the straightforward mathematical procedure of increasing each of its digits to the power of three and then adding the resultant sum, we arrive at the number 153. That is 1 to the power of 3; 5 to the power of 3 times 3 times three equals 1 125 27 153. Additionally, this may be written as 13 53 33=153.
1: Which of the following data types is immutable in Python?
Armstrong Number Logic
One must keep in mind that the Armstrong number characteristic holds true in all number systems in order to comprehend the logic of Armstrong nos. Take the six-digit number 548834 and check to see if it fits the Armstrong numbers property. Use the formula
5^6 4^6 8^6 8^6 3^6 4^6 =? Determine the amount by increasing each number to the power of six, then add the terms to determine the total. The sum, which is the actual number itself, is 548834. As a result, 548834 is an Armstrong number since it is the same when the individual terms of the number’s digits are added and raised to the power of the number’s digits. Consider the number 122 to see what Armstrong’s number is. Use the base-3 operation 13 23 23=17 to determine if 122 is an Armstrong no. The calculation is the same in base 3 as 2*1 2*3 1*9=17. It’s crucial to keep in mind that 3 to the power of 0 equals 1, 3 to the power of 1 equals 3, and 3 to the power of 2 equals 9. When adding all the words, we get 17. This means that, in any number system, an Armstrong number can display the same feature.
learn to code like an expert ! enroll now !
Armstrong Number Algorithm
An Armstrong number algorithm uses two parameters to implement and validate the characteristic of what an Armstrong number is. The number of digits in the number is the first parameter in Armstrong number logic, and the second parameter is the sum of the terms when each digit is raised to the power of the number’s digits. For a better understanding, consider the Armstrong number algorithm:
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 labeled an Armstrong number
Python Program To Check Armstrong Number
Implementing the aforementioned procedure in Python as shown below will help you understand what the Armstrong number is 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, keep in mind that num original keeps the initial value of the input, whereas num stores the number of digits in the input. Sum 1=0 and cnt=0 are equivalent to 1 Armstrong. One may calculate the sum of each digit in the starting number raised to the power of the count variable using the variable num 2. Next, compare the original and sum to check if the original number displays the characteristics of an Armstrong number according to the Python Armstrong number program.
Are you aspiring for a booming career in IT? If YES, then dive in |
||
Full Stack Developer Course |
Python Programming Course |
Data Science and Machine Learning Course |
Program To Find All The Armstrong Numbers Between 0 and 999
The Armstrong numbers list produced by this application ranges from 0 to 999. A number is said to be Armstrong if the sum of its digits raised to the power of three equals the number itself. For instance, the number 371 is an Armstrong number as 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
Learn Coding in your Language! Enroll Here!
Conclusion
Here, one may learn about the Armstrong number, its Python implementation, and its algorithm. Are there any real-world uses for the special quality of Armstrong numbers then? There aren’t any in reality, and the uniqueness of these numbers serves no useful use other than as an example or a teaching tool to help students validate their programs, understand topics better, and investigate the laws of a new programming language.
Our Other Courses | ||
MEP Course | Quantity Surveying Course | Montessori Teachers Training Course |
Performance Marketing Course | Practical Accounting Course | Yoga Teachers Training Course |