Online Tutorials & Training Materials | STechies.com
Register Login

Python Program to Check if a Number is Armstrong number or Not

|| 0

Python Program to Check if a Number is Armstrong number or Not
Stechies

What is Armstrong Number?

A three-digit number can se said to be an Armstrong number when the sum of all its individual digits cubes is equal to the number itself. 

A positive integer 'xyz' is Armstrong number if

xyz... = x^n + y^n + z^n ...

n = number of digits in the integer

For example  

3 digit Armstrong number 

3^3 + 7^3 + 0^3 = 370 

4 digit Armstrong number

1634 = 1^4 + 6^4 + 3^4 + 4^4

Some other example of Armstrong numbers are  0, 1, 2, 3, 153, 370, 407, 1634, 8208, etc.

In this tutorial, you will learn to write a python program to check whether a number is Armstrong number or not for 3 digits number aswell as N digit number.

Python Program to check 3 digit Armstrong Number

#Python program to check 3 Digits Armstrong Number 

#Taking input from user
num = int(input("Enter a number: "))
# Declarign and intilizing sum variable
arsum = 0
#Coping orignal value into a temp veriable
t = num

#While loop for iteration till t is greater than 0
while t > 0:
    #if Yes
    #Applying Modulation on number
    d = t % 10
    #Applying Formula sum = digit^3
    arsum += d ** 3
    t //= 10

#checking, Is original number equal to the result
if num == arsum:
    #if Yes, Than print It is an ARMSTRONG NUMBER
    print(num,"IS AN ARMSTRONG NUMBER")
else:
    #if NO, Than print It is not an ARMSTRONG NUMBER
    print(num,"IS NOT AN ARMSTRONG NUMBER") 

OUTPUT :

***PROGRAM TO CHECK WEATHER NUMBER IS ARMSTRONG IS NOT***
Enter a number: 370
370 IS AN ARMSTRONG NUMBER

Python Program to check N digit Armstrong Number

#Python program to check n Digits Armstrong Number 

#Taking input from user
number = int(input("Enter a number: "))

# Declarign and intilizing sum variable
result = 0

# Declarign and intilizing number of digits variable
n = 0

#coping number in another variable
originalNumber = number

#In this while loop checking the number of digits in entered number
#If number is greater than 0
while(originalNumber > 0) :
    #If Condition is true
    originalNumber = originalNumber//10
    #increasing the digit by 1 on every iteration
    n = n + 1

#Again coping entered number in another variable because we changed it
#during the process of getting the number of digits in number
originalNumber = number

#In this while loop we are applying the Formula
#geting the result on the basis of x^y (x=digit,y=total number of digit)
while(originalNumber > 0):
  #get the last digit of number
  reminder = originalNumber % 10
  #Applying multiplication of digit
  result = result + reminder ** n
  #removing the last digit from the number
  originalNumber = originalNumber // 10

#checking the output of program is equal to the entered number or not
if(result == number):
    #if Yes, Than it is a ARMSTRONG NUMBER
    print(number,"IS AN ARMSTRONG NUMBER")
else:
    #if No, Than it is not a ARMSTRONG NUMBER
    print(number,"IS NOT AN ARMSTRONG NUMBER") 

OUTPUT

***PROGRAM TO CHECK WEATHER NUMBER IS ARMSTRONG OR NOT***
Enter a number: 1634
1634 IS AN ARMSTRONG NUMBER