Learning coding means GreatToCode Be more than a Coder ! Greattocode , Join GreatToCode Community,1000+ Students Trusted On Us .If You want to learn coding, Then GreatToCode Help You.No matter what It Takes !


CODE YOUR WAY TO A MORE FULFILLING And HIGHER PAYING CAREER IN TECH, START CODING FOR FREE Camp With GreatToCode - Join the Thousands learning to code with GreatToCode
Interactive online coding classes for at-home learning with GreatToCode . Try ₹Free Per Month Coding Classes With The Top Teachers . Python program to check whether a given number is an Armstrong number or not:

Python program to check whether a given number is an Armstrong number or not:

Become More Then A coder | Learn & Start Coding Now.
An Armstrong number (also known as a narcissistic number, pluperfect digital invariant, or pluperfect number) is a number that is the sum of its own digits each raised to the power of the number of digits. For example, 153 is an Armstrong number because:

\[1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153\]

Here's a Python program to check whether a given number is an Armstrong number or not:


```python
def is_armstrong(number):
    # Convert the number to a string to find the number of digits
    num_str = str(number)
    num_digits = len(num_str)
    
    # Initialize the total sum
    total_sum = 0

    # Calculate the sum of digits raised to the power of the number of digits
    for digit_str in num_str:
        digit = int(digit_str)
        total_sum += digit ** num_digits

    # Check if the sum is equal to the original number
    return total_sum == number

# Example: Checking for Armstrong number
input_number = int(input("Enter a number: "))

if is_armstrong(input_number):
    print(f"{input_number} is an Armstrong number.")
else:
    print(f"{input_number} is not an Armstrong number.")
```

### Explanation:

1. The `is_armstrong` function takes an integer `number` as an argument and returns `True` if it's an Armstrong number, and `False` otherwise.

2. We convert the number to a string to find the number of digits using `len(num_str)`.

3. The function initializes `total_sum` to 0, which will store the sum of digits raised to the power of the number of digits.

4. We iterate through each digit in the number, convert it to an integer, and add it to the `total_sum` raised to the power of the number of digits.

5. Finally, we check if `total_sum` is equal to the original number. If they are equal, the number is an Armstrong number.

6. In the example, the user is prompted to enter a number using `input()`, and the `is_armstrong` function is called with the input number. The program then prints whether the number is an Armstrong number or not.

### Sample Output:


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

### Conclusion:

This Python program checks whether a given number is an Armstrong number using a simple algorithm. Understanding the concept of Armstrong numbers and implementing such checks can be useful in various mathematical and programming scenarios. Feel free to experiment with different inputs to test the program's functionality. Happy coding!

Post a Comment

0 Comments

•Give The opportunity to your child with GreatToCode Kid's • Online Coding Classes for Your Kid • Introduce Your kid To the world's of coding
•Fuel You Career with our 100+ Hiring Partners, Advance Your Career in Tech with GreatToCode. •Join The Largest Tech and coding Community and Fast Forward Your career with GreatToCode. •10000+ Learner's+ 90 % placement Guarantee. • Learning Coding is Better with the GreatToCode community .
•Greattocode Kid's •GreatToCode Career •GreatToCode Interview •GreatToCode Professional •GreatToCode for schools •GreatToCode For colleges •GreatToCods For Businesses.
Are you ready to join the millions of people learning to code? GreatToCode Pass is your one-stop-shop to get access to 1000+ courses, top-notch support, and successful job placement. What are you waiting for? Sign up now and get your future in motion with GreatToCode Pass.