Python program to check Armstrong number using functions

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*

Solution:-

  1. The user will enter the number and store it in the variable.
  2. The function will obtain each digit from the number and then convert it to string and store it in the list.
  3. The second map cube in which each digit is found and checked that is it equal to the original number.
  4. Then if the sum of the cube is equal to the original number then the number is Armstrong.
  5. Then at last final result is printed.

Steps:-

  1. The user enter any number
  2. Then counting of number is started as 370 means the 3.
  3. Then dividing numbers into the individual digit as 370 is divided into 3, 7, and 0.
  4. After that, we calculate the power of the n for each individual and then add those numbers.
  5. Then compare the original value with the sum value.
  6. If it matches then the number is Armstrong otherwise not.

Python program to check Armstrong number using functions Example:-

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

1) Program using the “while” loop:-

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

2) Program using the “for” loop:-

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

3) Program to find Armstrong number using int data type:-

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.

4) Program to find Armstrong number using the string data type:-

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.

5) Program to find Armstrong number using the recursion:-

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.

Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India