Python Program to Check Armstrong Number

PythonServer Side ProgrammingProgramming

In this article, we will learn about the solution and approach to solve the given problem statement.

Problem statement

Given an integer n , we need to check that the given integer is an armstrong number.

A positive integer is called an Armstrong number of order n if

abcd... = a^n + b^n + c^n + d^n + …

Here we will be discussing the brute-force approach for an armstrong number of 3 digits and hence of order three.

To check the armstrong number of order n we need to replace 3 by the corresponding order value in line number 7.

Now let’s see the implementation −

Example

 Live Demo

num = 221
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

221 is not an Armstrong number

All variables and functions are declared in the global scope as shown in the figure below.

Conclusion

In this article, we learned about the approach to check whether a given number is an armstrong number or not.

raja
Published on 26-Sep-2019 12:10:03
Advertisements