How to find the Number is Armstrong or Not in Python with Algorithm

 


In this post, we are going to make a program to find whether the number entered by the user is Armstrong or not in Python


Algorithm

1. We take input from the user in integer form.
2. We declare the sum as 0.
3. We declare an as num.
4. Now, we run while loop with some conditions
5. Now, we display the output as the number is Armstrong or not.

Rules

1. We take sum = 0
2. a is a num that is entered by the user.
3. while a is greater than 0
4. b is that when we divide a with 10 and the remainder is b.
5. sum += b ** 3 it mean sum = sum +b*b*b.
6. a // = 10 means to remove all the decimal values from the output.
7. If num and sum both are equal then print num is Armstrong.
8. Else print num is not Armstrong.

Code

num = int(input("Enter a number: "))
sum = 0
a = num
while a > 0:
   b = a % 10 
   sum += b ** 3 
   a //= 10

if num == sum:
   print(num,"is an Armstrong number")
else:
   print(num,"is not an Armstrong number")

Output

Enter a number: 1
1 is an Armstrong number
Enter a number: 20
20 is not an Armstrong number
Enter a number: 153
153 is an Armstrong number

Post a Comment

Previous Post Next Post