Examples
- Hello World Program in Python (How to Print Hello World in Python?)
- Python Program to Add Two Numbers (Sum/Addition Program)
- How to Generate Random Number in Python? (Program)
- Leap Year Program in Python (Check Leap Year or Not in Python)
- Python Program to Check Number is Positive, Negative, or Zero
- Even Odd Program in Python (How to Check Number is Even or Odd?)
- Python Program to Find Largest of 3 Numbers (Greatest of Three Numbers in Python)
- Prime Number Program in Python (Check Prime or Not)
- Python Program to Find Square Root of a Number (Examples)
- Python Factorial Program (How to Find Factorial of a Number in Python?)
- Python Program to Find Prime Numbers in Range
- Python Program to Find Area of Triangle (Different Ways With Examples)
- How to Solve Quadratic Equation in Python? (Program Examples)
- How to Swap Two Numbers in Python? (Program Examples to Swap Variables)
- Python Program to Convert Kilometers to Miles (km to mi) and Vice Versa
- Python Program to Convert Celsius To Fahrenheit (and Fahrenheit to Celsius)
- Program to Check Armstrong Numbers in Python (Code Examples)
- Program for Armstrong Number Between Two Intervals in Python
- Program to Print Multiplication Table in Python (Code Examples)
- Python Program for Fibonacci Series (Print Fibonacci Sequence in Python)
- Program for Sum of n Natural Numbers in Python (Code Examples)
- Program to Print Powers of 2 in Python (Code Examples)
- Python Program to Find Numbers Divisible by Another Number
- Convert Decimal to Binary, Octal and Hexadecimal in Python (Program with Example)
- How to Convert Decimal to Binary in Python? Program with Examples
- How to Convert Decimal to Octal in Python? Program with Examples
- How to Convert Decimal to Hexadecimal in Python? Program and Examples
- How to Convert Binary to Decimal in Python? Program With Examples
- How to Convert Hexadecimal to Octal in Python? Program With Examples
- Program to Find LCM of Two Numbers in Python (Calculate LCM in Python)
- GCD of Two Numbers in Python (Program to Find HCF or GCD)
- GCD of Three Numbers in Python (HCF Program for n Numbers)
- Simple Calculator Program in Python (Basic Calculator Code)
- Find Factors of a Number in Python (Programs and Examples)
- Program to Find Prime Factors of a Number in Python
- How to Find ASCII Value in Python? ASCII Value of Character
Program to Check Armstrong Numbers in Python (Code Examples)
Understanding how to check armstrong numbers in Python is a valuable skill for a lot of people. Whether you're a curious learner, a mathematical enthusiast, or a budding Python programmer, it can open doors to enhanced problem-solving skills and mathematical insights.
In this tutorial on armstrong numbers in Python, we will learn about these numbers and guide you through writing a program that effortlessly checks whether a given number is an Armstrong number or not.
We will understand the concept of armstrong number program in Python, highlight its benefits, and see some examples with different ways to write code for the armstrong number.
Also known as a narcissistic number, the armstrong number is a special kind of number where the sum of its individual digits raised to the power of the number of digits equals the original number itself.
For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.
Why is it important to learn about Armstrong numbers? First and foremost, Armstrong numbers nurtures our problem-solving abilities and strengthens our mathematical intuition. It allows us to exercise our logical thinking and algorithmic skills while diving into the realm of number theory.
Additionally, knowledge of Armstrong numbers finds practical use in various fields. One notable application is in error-checking mechanisms, especially in the context of data transmission or storage. By applying the concept of Armstrong numbers in Python, we can ensure data integrity by verifying the accuracy of transmitted or stored numbers.
Furthermore, Armstrong numbers serve as an engaging gateway to dive deeper into mathematical concepts such as number theory, digit manipulation, and even modular arithmetic. Understanding these underlying principles can prove beneficial in diverse areas, including cryptography, data analysis, and algorithmic problem-solving.
So, here, let’s get started and write a Python program to check armstrong number using different methods and techniques.
Armstrong Number Program in Python
Code
# Python program to check armstrong number or not
def is_armstrong_number(number):
# Convert the number to a string to calculate the number of digits
num_str = str(number)
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 == number:
return True
else:
return False
# Example usage
number = int(input("Enter a number: "))
if is_armstrong_number(number):
print(f"{number} is an Armstrong number.")
else:
print(f"{number} is not an Armstrong number.")
Output
Enter a number: 1634
1634 is an Armstrong number.
Explanation
-
We define a function is_armstrong_number that takes a number as input and checks whether it is an Armstrong number or not.
-
Inside the function, we convert the number to a string using str(number) to calculate the number of digits. Then, we calculate the sum of each digit raised to the power of the number of digits using a generator expression and the sum function.
-
Finally, we compare the sum with the original number and return True if they are equal, indicating that the number is an Armstrong number. Otherwise, we return False.
-
In the example, we take a number as input from the user and call the is_armstrong_number function. We then display the corresponding message based on the result.
Program for 4 Digit Armstrong Number in Python
Code
# Python program to print 4 digit armstrong numbers
def is_armstrong_number(number):
num_str = str(number)
num_digits = len(num_str)
sum_of_digits = sum(int(digit) ** num_digits for digit in num_str)
if sum_of_digits == number:
return True
else:
return False
print("4-digit Armstrong numbers:")
for number in range(1000, 10000):
if is_armstrong_number(number):
print(number)
Output
4-digit Armstrong numbers:
1634
8208
9474
Explanation
-
In this program, we use the is_armstrong_number function to check whether a number is an Armstrong number or not.
-
We iterate over all numbers in the range from 1000 to 9999 using a for loop. These are the 4-digit numbers.
-
For each number in the range, we call the is_armstrong_number function to determine if it is an Armstrong number. If it is, we print the number.
By running this program, it will find and print all 4-digit Armstrong numbers in Python.
Practice More Python Programs:
- Hello World Program in Python
- Python Program to Add Two Numbers
- Generate Random Number in Python
- Leap Year Program in Python
- Even Odd Program in Python
- Python Program to Find Largest of 3 Numbers
- Prime Number Program in Python
- Python Program to Find Square Root of a Number
- Python Factorial Program
- Python Program to Find Area of Triangle
- Solve Quadratic Equation in Python
- Swap Two Numbers in Python
- Python Program to Convert Kilometers to Miles
- Python Program to Convert Celsius To Fahrenheit
- Print Multiplication Table in Python
- Python Program for Fibonacci Series
- Convert Decimal to Binary, Octal and Hexadecimal in Python
- Find LCM of Two Numbers in Python
- GCD of Two Numbers in Python
- Simple Calculator Program in Python
- Python Matrix Addition Program
- Transpose of Matrix in Python
- Python Matrix Multiplication
- Python Calendar Program