Thursday, 9 July 2020

Python program to check Armstrong number

                   Check Armstrong

Write a Program to determine if the given number is Armstrong number or not. Print true if number is armstrong, otherwise print false.

An Armstrong number is a number (with digits n) such that the sum of its digits raised to nth power is equal to the number itself.
For example,
371, as 3^3 + 7^3 + 1^3 = 371
1634, as 1^4 + 6^4 + 3^4 + 4^4 = 1634

Input Format :
Integer n
Output Format :
true or false

CODE:

## Read input as specified in the question.
## Print output as specified in the question.
def armstrong(n):
    d = 0
    temp = n
    while temp > 0:
        temp = temp // 10
        d = d + 1
    arm = 0
    while n > 0:
        rem = n % 10
        arm = arm + rem ** d 
        n = n // 10
    return arm

n = int(input())
temp = n
x = armstrong(n)
if temp == x:
    print("true")
else:
    print("false")


TEST CASE:
Sample Input 1 :
1
Sample Output 1 :
true
Sample Input 2 :
103
Sample Output 2 :
false


No comments:

Post a Comment