Python Program to Check Armstrong Number

By | October 3, 2021
Python Program to Check Armstrong Number

In this python program, we will ask the user to enter a number and as an output, the program will tell if the entered number is an Armstrong number or not.

Armstrong Number:

A number is said to be Armstrong’s number if the sum of that number each digit which is raised to the power of total digits present in that number is equal to the number itself.

for instance: 153 is an Armstrong number because:

153 = (1)3 + (5)3 +(3)3

Vamware

Prerequisite topics to create this program.

  • Python Input, Output
  • Python Data types
  • Python Type Conversion
  • Python loop
  • Python Arithmetic and comparisons Operators
Steps
  • First, we will ask the user to enter a number
  • now using the len() function we compute the total digit present in the entered number.
  • now using the for loop we will go through each digit present in the number and raise it to the power of the total length of the number, and simultaneously we will also compute its sum.

Python Program to Check Armstrong Number:

Code:

Vamware
n = input("Enter a number: ")    # n is a string not a number

power = len(n)
sum=0

#loop through a string
for i in n:
    sum += int(i)**power

if sum == int(n):
    print(n + " is an Armstrong Number")
else:
    print(n+" is not an Armstrong Number")

Output 1:

Enter a number: 1634
1634 is an Armstrong Number

Output 2:

Enter a number: 23
23 is not an Armstrong Number

People are also reading:

Author: Vinay

I am a Full Stack Developer with a Bachelor's Degree in Computer Science, who also loves to write technical articles that can help fellow developers.

Leave a Reply

Your email address will not be published.