Armstrong number is a number whose sum of the n^{th} power of digits is equal to the number itself. Where n is called the order, which is equal to the number of digits present in the number.

**For example:**

Let’s take a number 371, which is an Armstrong number. Let’s see

Order of number 371 (n) = 3

371 = 3^{3} + 7^{3} + 1^{3}

Let’s take one more example of number 1634

Order of number 1634 (n) = 4

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

Write a program to Check Armstrong Number in Python

Code:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# Python program to check if the number provided by the user is an Armstrong number or not # take input from the user num = int(input("Enter a number: ")) # initialize sum sum = 0 # find the sum of the cube of each digit temp = num while temp > 0: digit = temp % 10 sum += digit ** 3 temp //= 10 # display the result if num == sum: print(num,"is an Armstrong number") else: print(num,"is not an Armstrong number") |

This Python program takes an input number from the user and determines if it is an Armstrong number or not. An Armstrong number, also known as a narcissistic number, is a number that is equal to the sum of its own digits each raised to the power of the number of digits. For example, 153 is an Armstrong number since 153 = 1^3 + 5^3 + 3^3

The program starts by asking the user to input a number, which is stored as an integer in the variable “num”.

Then, the program initializes a variable “sum” to 0 and uses a while loop to iterate through the digits of the input number. On each iteration, it calculates the cube of the current digit, adds it to the variable “sum” and divides the input number by 10 to move to the next digit.

Finally, it checks if the input number is equal to the sum of the cubes of its digits. If it is, it prints “num is an Armstrong number” to the output, where num is the input number. If it is not, it prints “num is not an Armstrong number” to the output.

The output will be a string indicating whether the input number is an Armstrong number or not.

## Check Armstrong number by taking the number as int datatype

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# taking input and storing in num variable num = int(input("Enter a number: ")) # finding order using len() and str() function order = len(str(num)) # initializing the sum variable by 0 sum = 0 # storing the num in temp variable temp = num # going through every digit of number store in temp variable while temp > 0: n = temp % 10 temp = temp // 10 # storing the sum of cube of the digits in sum variable sum = sum + pow(n,order) # checking whether the sum is equal to num or not if(sum == num): # printing the num is an Armstrong number print(num,"is an Armstrong Number.") else: # printing the num is not an Armstrong number print(num,"is not an Armstrong Number.") |

Output:

1 2 3 4 |
Enter a number: 371 371 is an Armstrong Number. |

This Python program takes an input number from the user and determines if it is an Armstrong number or not. An Armstrong number, also known as a narcissistic number, is a number that is equal to the sum of its own digits each raised to the power of the number of digits. For example, 153 is an Armstrong number since 153 = 1^3 + 5^3 + 3^3

The program starts by asking the user to input a number, which is stored as an integer in the variable “num”.

Then, it uses the built-in Python functions len() and str() to find the number of digits in the input number and stores it in the variable “order”.

It initializes the variable “sum” to 0 and stores the input number in a variable “temp”

Then, the program uses a while loop to iterate through the digits of the input number. On each iteration, it calculates the power of the current digit, raise to the order of the digits of the number, adds it to the variable “sum” and divides the temp variable by 10 to move to the next digit.

Finally, it checks if the input number is equal to the sum of the power of the digits of the input number. If it is, it prints “num is an Armstrong number” to the output, where num is the input number. If it is not, it prints “num is not an Armstrong number” to the output.

The output will be a string indicating whether the input number is an Armstrong number or not.

## Check Armstrong number by taking the number as string datatype

The idea is same as above but the program is a little bit short and also the process.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# taking input and storing in num variable num = input("Enter a number: ") # finding order using len() function order = len(num) # initializing the sum variable by 0 sum = 0 # going through every digit of number store in num variable for i in num: # storing the sum of cube of the digits in sum variable sum += pow(int(i),order) # checking whether the sum is equal to num or not if sum == int(num): # printing the num is an Armstrong number print(num,"is an Armstrong Number.") else: # printing the num is not an Armstrong number print(num,"is not an Armstrong Number.") |

Output:

1 2 3 4 |
Enter a number: 371 371 is an Armstrong Number. |

This Python program takes an input number from the user as a string and determines if it is an Armstrong number or not. An Armstrong number, also known as a narcissistic number, is a number that is equal to the sum of its own digits each raised to the power of the number of digits. For example, 153 is an Armstrong number since 153 = 1^3 + 5^3 + 3^3

The program starts by asking the user to input a number, which is stored as a string in the variable “num”.

Then, it uses the built-in Python function len() to find the number of digits in the input number and stores it in the variable “order”.

It initializes the variable “sum” to 0.

Then, the program uses a for loop to iterate through the digits of the input number, stored as a string. On each iteration, it calculates the power of the current digit, raise to the order of the digits of the number, adds it to the variable “sum” .

Finally, it checks if the sum is equal to the input number as integer. If it is, it prints “num is an Armstrong number” to the output, where num is the input number. If it is not, it prints “num is not an Armstrong number” to the output.

The output will be a string indicating whether the input number is an Armstrong number or not.

## Check Armstrong number by using recursion

In this, we have created a getSum() recursive function which will return the sum of the n^{th} power of digits of the number. Rest is almost same.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# defining the getSum() recursive function def getSum(num): if num == 0: # Base case return num else: # Iterative case return pow((num%10),order) + getSum(num//10) # taking input and storing in num variable num = int(input("Enter a number: ")) # finding order using len() and str() function order = len(str(num)) # initializing the sum variable by getting the sum from getSum() sum = getSum(num) # checking whether the sum is equal to num or not if sum == int(num): # printing the num is an Armstrong number print(num,"is an Armstrong Number.") else: # printing the num is not an Armstrong number print(num,"is not an Armstrong Number.") |

Output:

1 2 3 4 |
Enter a number: 371 371 is an Armstrong Number. |

This Python program takes an input number from the user and determines if it is an Armstrong number or not. An Armstrong number, also known as a narcissistic number, is a number that is equal to the sum of its own digits each raised to the power of the number of digits. For example, 153 is an Armstrong number since 153 = 1^3 + 5^3 + 3^3.

The program starts by defining a function called “getSum(num)” that takes an input number as a parameter. This function uses recursion to find the sum of each digit of the input number raised to the power of the number of digits of the input number. The function uses an if-else statement to check if the input number is equal to zero, which serves as the base case of the recursion. If the input number is equal to zero, the function returns 0. If the input number is not equal to zero, the function returns the power of the last digit of the input number plus the result of calling the function again with the input number divided by 10 (to remove the last digit).

Then, the program takes an input number from the user and stores it as an integer in the variable “num”. It uses the built-in Python functions len() and str() to find the number of digits in the input number and stores it in the variable “order”.

It initializes the variable “sum” by calling the “getSum(num)” function and passing “num” as an argument.

Finally, it checks if the sum is equal to the input number, if it is, it prints “num is an Armstrong number” to the output, where num is the input number. If it is not, it prints “num is not an Armstrong number” to the output.

The output will be a string indicating whether the input number is an Armstrong number or not.