Practical 7: Program to Find Whether the Given Number is an Armstrong Number or Not
Problem Statement
Write a Python program to check whether a given number is an Armstrong number or not.
An Armstrong number (or narcissistic number) is a number that is equal to 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 = 153
- 370 is an Armstrong number because: 3^3 + 7^3 + 0^3 = 370
The program should:
- Take a number as input from the user.
- Check if the number is an Armstrong number.
- Print whether the number is an Armstrong number or not.
Solution Code
# Program to check if the given number is an Armstrong number
# Input: Take an integer number from the user
num = int(input("Enter a number: "))
# Convert the number to string to count the number of digits
num_str = str(num)
num_digits = len(num_str)
# Calculate the sum of each digit raised to the power of the number of digits
sum_of_digits = sum(int(digit) ** num_digits for digit in num_str)
# Check if the sum is equal to the original number
if sum_of_digits == num:
print(f"{num} is an Armstrong number.")
else:
print(f"{num} is not an Armstrong number.")
Output
- The program will ask the user to input a number.
- It will then check if the entered number is an Armstrong number.
- The program will print whether the number is an Armstrong number or not.
Example outputs for different inputs:
For Armstrong number:
Enter a number: 370
370 is an Armstrong number.
Process finished with exit code 0
For Non Armstrong number:
Enter a number: 370
370 is an Armstrong number.
Process finished with exit code 0
Explanation
Below is the explanation of F.E. PPS Unit 2 Practical 7 solution where we have written Python Program to Find Whether the Given Number is an Armstrong Number or Not.
- Input of Number:
- The program prompts the user to enter a number, which is then stored in the variable num.
- Counting the Digits:
- The number is converted to a string using str(num) so that we can easily iterate over each digit. The length of the string gives us the number of digits in the number, which is stored in num_digits.
- Calculating the Sum of Digits Raised to the Power of the Number of Digits:
- A generator expression is used to iterate over each digit in the string. For each digit, it is converted back to an integer, raised to the power of num_digits, and summed up using the sum() function.
- Armstrong Check:
- After calculating the sum of the digits raised to the power of the number of digits, the program checks if the sum is equal to the original number. If they are equal, the number is an Armstrong number; otherwise, it is not.
- Displaying the Result:
- Based on the comparison, the program prints whether the number is an Armstrong number or not.
Key Concept Learned
- String Manipulation:
- The program demonstrates how to convert a number to a string to easily iterate over its digits. This is useful for problems involving individual digits of a number.
- Exponentiation in Python:
- The program uses exponentiation (**) to raise each digit to the power of the number of digits, which is a key operation for checking Armstrong numbers.
- Using sum() with Generator Expressions:
- The use of generator expressions with the sum() function allows efficient calculation of the sum of the digits raised to a power.
- Conditional Logic:
- The program uses conditional statements (if-else) to check whether the number is an Armstrong number, helping to reinforce decision-making in Python.