Python program to check Armstrong number using functions : In this tutorial we are going to write the program to check the number is Armstrong number or not using the function. The given number is equal to the sum of the nth power of each digit in that integer then the number is called Armstrong number in the python language. The number 370 is called the Armstrong number because the individual digit is 3. The next definition is that if the number is equal to the sum of cubes of own digits called as Armstrong number. The Armstrong number is also called as the narcissistic number. Suppose the number is 153 has the 1*1*1+5*5*5+3*3*
Steps:-
num=int (input (“Enter a number :”))
sum=0
temp=num
while temp>0:
digit=temp%10
sum+=digit**3
temp//=10
if num==sum:
print(num,”is an Armstrong number”)
else:
print(num,”is not an Armstrong number”)
Output:-
Enter a number: 153
153 is an Armstrong number
The python program will allow the user to enter the positive integer and check the given number is Armstrong or not using the while loop.
The Armstrong number will allow the user to enter any positive integer then the number is assigned to the variable.
We can assign the original value to the temp variable and help to preserve the original value than do the manipulation on the temp variable.
The while loop will make confirm that the given number is greater than 0.
The statements which are present in the while loop are split into the numbers and the count inside the given number.
Example:-
number=int (input (“\n Please enter the number to check for Armstrong :”))
Times=0
Temp=Number
While Temp>0:
Times=Times+1
Temp=Temp//10
Temp=Number
While Temp>0:
Reminder=Temp%10
Sum=sum+ (Reminder **Times)
Temp//=10
if Number==sum:
print(“\n %d is Armstrong Number.\n”%Number)
else:
print(“\n %d is Not a Armstrong Number.\n”%Number)
Output:-
Please enter the number to check for Armstrong: 123
123 is not san Armstrong number
The python program that allow the user to enter in positive integer and the program will check the number of the Armstrong number or not using the “for loop”.
Example:-
number=int (Input (“\n Please enter the number to check for Armstrong :”))
sum=0
Times=0
Temp=Number
While Temp>0:
Times=Times+1
Temp=Temp//10
Temp=Number
for n in range (1, Temp+1):
reminder=Temp%10
sum=sum+ (Reminder**Times)
Temp//=10
if Number==sum:
print(“\n %d is Armstrong number.\n”%Number)
else:
print(“\n %d is Not a Armstrong number.\n”%Number)
Output:-
Please enter the number to check for Armstrong: 370
370 is an Armstrong number
The int data type is taken and we find the order of the number and the last passed number to loop process digit by digit.
Example:-
num=int (input (“Enter a number :”))
order=len (str (num))
sum=0
Temp=num
While temp>0:
N=temp%10
Temp=temp//10
sum=sum+pow (n, order)
if(sum=num):
print(num,”is an Armstrong Number.”)
else:
print(num,”is not an Armstrong Number.”)
Output:-
Enter a number: 371
371 is an Armstrong Number.
The program is written using the string as the data type.
Example:-
num= (input (“Enter a number :”)
order=len (num)
sum=0
for i in num:
sum+=pow (int (i), order)
if sum=int(num):
print(num,”is an Armstrong Number.”)
else:
print(num,”is not an Armstrong Number.”)
Output:-
Enter a number: 1
1 is an Armstrong Number.
The getsum () is the recursive function which return sum of the nth power of digit and the rest is same.
Example:-
def getSum (num):
if num=0:
return num
else:
return num
else:
return pow ((num%10), order) +get Sum (num//10)
num=int (input (“Enter a number :”))
order=len (str (num))
sum=getSum (num)
if sum=int (num):
print(num,”is an Armstrong Number.”)
else:
print(num,”is not an Armstrong Number.”)
Output:-
Enter a number: 1634
1634 is an Armstrong Number.