python program
Write a program in python to check a number is Armstrong or not.
Program for Check a number is Armstrong or not.
Before start the programming, we need to know that, what is Armstrong Number?
An Armstrong number is a number such that the sum of its all digits raised to the power of 'total no of digit' is equal to the number itself
Steps of programming:-
- get input from user (e.g- int x).
- Store that no in another variable because after calculation we need to compair the calculated value to the inputted value (e.g- y=x).
- initialize two variable with zero (0) because we will store result in these variable during the loop (e.g- a=0 & z=0).
- Calculate length of inputted integer value (e.g- let 'l' is length)
- Start a loop till inputted value becomes zero and decrements with divided by 10 (means x=x/10).
- In loop calculate mod (%)10 of inputted value and store in a variable which is initialize with zero (e.g- z=x%10).
- And after it calculate z*i and store in another variable (e.g- a=a+(z**i)).
- Then we compare the calculated value to the inputted value if both are same then we can say that the inputted value is Armstrong else not.
code:-
x=int(input("Enter number : "))
y=x
a=0
z=0
l=len(str(x))
while x!=0:
z=x%10
a=a+(z**l)
x=x//10
if y==a:
print(y,"is an armstrong number")
else:
print(y,"is not an armstrong number")
What will be done in the loop section ?
In the loop section means after while statement, 1st it will check, is x is zero or not, if x =0 then loop will break else z=x%10 and after it a=a+(z**l) means in 'a' it will store l'th power of z and then x=x//10. after it loop will start again and check is x = 0 if not then work again till x=0.
#DizIT_Operators
No comments:
Post a Comment