Python

Python Program to Check Armstrong Number8 min read

Armstrong number is a number whose sum of the nth 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 = 33 + 73 + 13

Let’s take one more example of number 1634

Order of number 1634 (n) = 4

1634 = 14 + 64 + 34 + 44

Write a program to Check Armstrong Number in Python

Code:

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

Output:

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.

Output:

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 nth power of digits of the number. Rest is almost same.

Output:

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.

Leave a Comment