Algorithm


1. Initialize an empty list armstrong_numbers.
2. For each number in the range from 100 to 999 (inclusive):
    a. Convert the number to a string to find the number of digits.
    b. Calculate the sum of the cube of each digit.
    c. Check if the calculated sum is equal to the original number.
    d. If the sum is equal, add the number to the armstrong_numbers list.
3. Print or return the armstrong_numbers list.

Code Examples

#1 Example- Python Program to Check Armstrong Number Using While Loop

Code - Python Programming

number = 153
temp = number
add_sum = 0
while temp != 0:
    k = temp % 10
    add_sum += k*k*k
    temp = temp//10
if add_sum == number:
    print('Given number is a three-digit Armstrong Number')
else:
    print('Given number is not an Armstrong Number')

Copy The Code & Try With Live Editor

Output

x
+
cmd
Given number is a three-digit Armstrong Number

#2 Example- Python Program to Find an n-digit Armstrong Number by Using While Loop

Code - Python Programming

number = 1634
digits = len(str(number))
temp = number
add_sum = 0
while temp != 0:
    # get the last digit in the number
    k = temp % 10
    # find k^digits
    add_sum += k**digits
    # floor division
    # which update number with second last digit as last digit
    temp = temp//10
if add_sum == number:
    print('Given number is an Armstrong Number')
else:
    print('Given number is not a Armstrong Number')

Copy The Code & Try With Live Editor

Output

x
+
cmd
Given number is an Armstrong Number

#3 Example- Python Program to Find an n-digit Armstrong Number by Using Using Functions

Code - Python Programming

def count_digits(n):
   i = 0
   while n > 0:
      n //= 10
      i += 1
   return i

def sum(n):
   i = count_digits(n)
   s = 0
   while n > 0:
      digit = n%10
      n //= 10
      s += pow(digit,i)
   return s


num = 1634

# calling the sum function
s = sum(num)

# check armstrong number or not
if s == num:
   print('Given number is an Armstrong Number')
else:
   print('Given number is not an Armstrong Number')

Copy The Code & Try With Live Editor

Output

x
+
cmd
Given number is an Armstrong Number

#4 Example- Python Program to Find an n-digit Armstrong Number by Using Recursion

Code - Python Programming

def check_armstrong(num,n1,sum,temp):
    if temp==0:
        if sum==num:
            return True
        else:
            return False
    digit = temp % 10
    sum = sum + digit**n1
    temp = temp//10
    return check_armstrong(num, n1, sum, temp)

num = 1634
sum = 0
n1 = len(str(num))
temp = num
res = check_armstrong(num,n1,sum,temp)

if res:
   print("Given number is an Armstrong Number")
else:
   print("Given number is not an Armstrong Number")
Copy The Code & Try With Live Editor

Output

x
+
cmd
Given number is an Armstrong Number
Advertisements

Demonstration


Python Programing Example to Check Armstrong Number-DevsEnv