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 for Armstrong Number Between Two Intervals in Python
Wondering how to find armstrong number between two intervals in Python? Well, you have landed on the right tutorial here. We are going to see practical examples of it, whether you want to find armstrong numbers between 1 to 10000 in Python or check the armstrong numbers in a given range.
These programs are very useful for mathematics enthusiasts, Python programmers and developers, as well as aspiring tech geeks. Learning to find Armstrong numbers in an interval using Python opens up a multitude of exciting possibilities for honing your skills and unraveling the mysteries of number theory.
An Armstrong number, also known as a narcissistic 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 instance, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 equals 153.
The ability to find Armstrong numbers within a range has practical applications in various fields. For example, in the domain of data validation or error-checking mechanisms, identifying Armstrong numbers can be used to ensure data integrity and accuracy during data transmission or storage.
Additionally, Armstrong numbers serve as a fascinating gateway to delve deeper into mathematical concepts such as digit manipulation, modular arithmetic, and mathematical patterns. By honing your skills in identifying Armstrong numbers, you develop a stronger foundation for tackling more advanced mathematical problems and exploring related areas, including cryptography, algorithmic problem-solving, and data analysis.
By leveraging Python's versatility, we can efficiently scan and validate a range of numbers, minimizing the risk of errors. So, let’s get started!
Find Armstrong Number Between Two Intervals in Python
Code
# Python program for armstrong number between two intervals
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
# Example usage
start = int(input("Enter the starting number: "))
end = int(input("Enter the ending number: "))
print(f"Armstrong numbers between {start} and {end}:")
for number in range(start, end + 1):
if is_armstrong_number(number):
print(number)
Output
Enter the starting number: 100
Enter the ending number: 500
Armstrong numbers between 100 and 500:
153
370
371
407
Explanation
-
We define the function is_armstrong_number to check whether a number is an Armstrong number or not, just like in our tutorial on Python program to find armstrong number or not.
-
In the example section, we prompt the user to enter the starting and ending numbers for the desired interval to search for Armstrong numbers. We then iterate over each number within that interval using a for loop.
-
For each number, we call the is_armstrong_number function to determine if it is an Armstrong number. If it is, we print the number.
Find Armstrong Numbers Between 1 to 10000 in Python
Code
# Python program to print all Armstrong numbers between 1 and 10,000
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("Armstrong numbers between 1 and 10,000:")
for number in range(1, 10001):
if is_armstrong_number(number):
print(number)
Output
Armstrong numbers between 1 and 10,000:
1
2
3
45
6
7
8
9
153
370
371
407
1634
8208
9474
Explanation
-
In this program, we have the is_armstrong_number function which checks whether a number is an Armstrong number or not.
-
We use a for loop to iterate over each number in the range from 1 to 10,000. For each number, we call the is_armstrong_number function to determine if it is an Armstrong number. If it is, we print the number.
Armstrong Number in Python Using for loop
Code
# Armstrong numbers between two intervals in Python using for loop
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
# Example usage
start = int(input("Enter the starting number: "))
end = int(input("Enter the ending number: "))
print(f"Armstrong numbers between {start} and {end}:")
for number in range(start, end + 1):
if is_armstrong_number(number):
print(number)
Output
Enter the starting number: 1
Enter the ending number: 100
Armstrong numbers between 1 and 100:
1
2
3
4
5
6
7
8
9
Explanation
-
In this program, we reuse the is_armstrong_number function from the previous example to check whether a number is an Armstrong number or not.
-
In the example, we prompt the user to enter the starting and ending numbers for the range of values to check. Then, we iterate over each number within that range using a for loop.
-
For each number, 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 display all Armstrong numbers within the specified range using a for loop.
Armstrong Number in Python Using while loop
Code
# Armstrong numbers between two intervals in python using while loop
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
# Example usage
start = int(input("Enter the starting number: "))
end = int(input("Enter the ending number: "))
print(f"Armstrong numbers between {start} and {end}:")
number = start
while number <= end:
if is_armstrong_number(number):
print(number)
number += 1
Output
Enter the starting number: 100
Enter the ending number: 999
Armstrong numbers between 100 and 999:
153
370
371
407
Explanation
-
In this program, we use the is_armstrong_number function from the previous examples to check whether a number is an Armstrong number or not.
-
In the example, we prompt the user to enter the starting and ending numbers for the range of values to check. We initialize the variable number with the starting value.
-
Using a while loop, we continuously iterate as long as the number is less than or equal to the ending value. Within each iteration, we check if the current number is an Armstrong number using the is_armstrong_number function. If it is, we print the number.
-
After each iteration, we increment the number by 1 to move to the next number in the range.
By running this program, it will find and display all Armstrong numbers within the specified range using a while loop.
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
- Check Armstrong Numbers in Python
- 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