Write a program to print the Armstrong numbers between 100 to 999
This program is checking for all the 3-digit numbers (100-999) if they are Armstrong numbers or not.
- An Armstrong number, also known as a narcissistic number, is a number that is equal to the sum of its own digits each raised to the power of the number of digits.
- The program starts with a for loop that iterates through all the numbers from 100-999. For each number, the program initiates a variable 'sum' to store the sum of the cubes of the digits of the number.
- Then it enters a while loop where it takes the last digit of the number by using the modulus operator, cubes it and adds it to the sum. Then it updates the number by using integer division operator to remove the last digit. This process repeats until all digits of the number have been processed.
- After the while loop, it checks if the sum is equal to the original number or not. If it is, then it is an Armstrong number and it prints the number and the message "is a Armstrong Number", otherwise, it prints the number and the message "is a Not Armstrong Number".
Source Code
for i in range(100,999):
a=i
sum=0
while(i>0):
rem=i%10
sum=sum+(rem*rem*rem)
i=i//10
if(sum==a):
print(a, "is a Armstrong Number")
Output
153 is a Armstrong Number
370 is a Armstrong Number
371 is a Armstrong Number
407 is a Armstrong Number