Write a Python Program to Check Armstrong Number?

Write a Python Program to Check Armstrong Number?

Armstrong Number:

It is a number that is equal to the sum of its own digits, each raised to a power equal to the number of digits in the number.

For example, let's consider the number 153:
It has three digits (1, 5, and 3).
If we calculate
1 3+ 5 3 + 33, we get1+125+27, which is equal to.

So, 153 is an Armstrong number because it equals the sum of its digits raised to the power of the number of digits in the number.
Another example is 9474:
It has four digits (9, 4, 7, and 4).
If we calculate 9 4 + 4 4 + 7 4 + 4 4,we get 6561+256+2401+256 , which is also equal to 9474

Therefore, 9474 is an Armstrong number as well.
Program
def myfun():
    num=int(input("Enter a number: "))
    
    # Calculate the number of digits in num
    num_str=str(num)
    num_digits=len(num_str)
    
    # Initialize variables
    sum_of_powers=0
    temp_num=num
    
    # Calculate the sum of digits raised to the power of num_digits
    while(temp_num>0):
        digit=temp_num%10
        sum_of_powers = sum_of_powers + digit ** num_digits
        temp_num = temp_num // 10

    # Check if it's an Armstrong number
    if(sum_of_powers==num):
        print(f"{num}, is an Armstrong number.")
    else:
        print(f"{num}, is not an Armstrong number.")
if __name__=="__main__":
    myfun()



Output:

Enter a number: 9474
9474, is an Armstrong number.


Enter a number: 153
153, is an Armstrong number.



More Questions


70 . Write a Python Program to Print the Fibonacci sequence.
71 . Write a Python Program to Check Armstrong Number?
72 . Write a Python Program to Find Armstrong Number in an Interval.
73 . Write a Python Program to Find the Sum of Natural Numbers.
74 . Write a Python Program to Find LCM.
75 . Write a Python Program to Find HCF.
76 . Write a Python Program to Convert Decimal to Binary, Octal and Hexadecimal.
77 . Write a Python Program To Find ASCII value of a character.
78 . Write a Python Program to Make a Simple Calculator with 4 basic mathematical operations.
79 . Write a Python Program to Display Fibonacci Sequence Using Recursion.
80 . Write a Python Program to Find Factorial of Number Using Recursion.



For latest job updates join Telegram Channel: https://t.me/sateeshm