Find the Armstrong Numbers between Two Intervals using Python
Find the Armstrong Number in a given Range in Python
Given two integers high and low for limits as inputs, the objective is to write a code to Find the Armstrong Numbers in a given Interval in C++.
For Instance, Input : 150 160 Output : 153
Find the Armstrong Numbers in a given Range in Python
Given two integer inputs as intervals high and low, the objective is to write a python code to check if the numbers lying within the given interval are Armstrong Numbers or not.
An Armstrong number or a Narcissistic number is any number that sums up itself when each of its digits is raised to the power of a total number of digits in the number. Let us try to understand this through the below example,
- abcd… = an + bn + cn + dn + …
- Where n is the order(length/digits in number)
Let’s look at some examples of Armstrong Numbers.
Examples
Here are few examples that’ll help you to understand the concept better.
370 = 3^3 + 7^3 + 0^3
= 27 + 343 + 0
= 370
1634 = 1^4 + 6^4 + 3^4 + 4^4
= 1 + 1296 + 81 + 256
= 1634
Method 1
Python Code
low, high = 10, 10000 for n in range(low, high + 1): # order of number order = len(str(n)) # initialize sum sum = 0 temp = n while temp > 0: digit = temp % 10 sum += digit ** order temp //= 10 if n == sum: print(n, end=", ")
Method 2
Python Code
import math first, second = 150, 10000 def is_Armstrong(val): sum = 0 # this splits the val into its digits # example val : 153 will become [1, 5, 3] arr = [int(d) for d in str(val)] # now we iterate on array items (digits) # add these (digits raised to power of len i.e order) to sum for i in range(0, len(arr)): sum = sum + math.pow(arr[i], len(arr)) # if sum == val then its armstrong if sum == val: print(str(val) + ", ", end="") for i in range(first, second + 1): is_Armstrong(i)
Output
153, 370, 371, 407, 1634, 8208, 9474,
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python
Login/Signup to comment
first=int(input(“Enter the first number:=”))
end=int(input(“Enter the last number:=”))
def arm(first,end):
result=[]
for i in range(first,end+1):
sum=0
val=[int(d) for d in str(i)]
n=len(val)
for j in val:
sum+=j**n
if i==sum:
result.append(sum)
print(result)
arm(first,end)
start = int(input(‘Enter starting number: ‘))
end = int(input(‘Enter ending number: ‘))
print(‘Amstrong between {} and {} are ‘.format(start,end))
for i in range(start,end):
num = [int(a) for a in str(i)]
sum=0
for k in range(len(num)):
sum = sum + pow(num[k],len(num))
if sum==i:
print(sum,end=’ ‘)
a=int(input(‘Enter first interval’))
b=int(input(‘Enter Ending interval’))
for i in range(a,b+1):
sum = 0
temp=i
order = len(str(i))
while(i>0):
rem=i%10
sum=sum+rem**order
i=i//10
if(temp==sum):
print(temp,’ number is Armstrong’)
else:
print(temp,’ number is not Armstrong’)
output
———————
Enter first interval150
Enter Ending interval160
150 number is not Armstrong
151 number is not Armstrong
152 number is not Armstrong
153 number is Armstrong
154 number is not Armstrong
155 number is not Armstrong
156 number is not Armstrong
157 number is not Armstrong
158 number is not Armstrong
159 number is not Armstrong
160 number is not Armstrong
n=int(input(“Enter first number : “))
m=int(input(“Enter second number : “))
for num in range(n, m + 1):
order = len(str(num))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10
if num == sum:
print(num,”is Armstrong Number”)
else:
print(num,”is not an Armstrong number”)
num = str(input())
lst = [(int(i)) ** 3 for i in num]
print(True) if sum(lst) == int(num) else print(False)
a=int(input())
b=int(input())
for i in range(a,b+1):
a1=str(i)
n=len(a1)
sum=0
for j in a1:
sum=sum+int(j)**n
if sum==int(i):
print(i)
n=int(input(“enter 1st no”))
m=int(input(“enter 2nd no”))
for x in range(n,m+1):
temp=x
sum=0
while temp>0:
r=temp%10
sum=sum+r**3
temp=temp//10
if sum==x:
print(“armstrong no”,sum)
start=int(input(“enter a start no:”))
end=int(input(“enter a end no:”))
for n in range(start,end+1):
sum=0
num=n
while(num>0):
rem=num%10
sum=sum+(rem**3)
num=num//10
if(n==sum):
print(n)
start=int(input(“enter a start no:”))
end=int(input(“enter a end no:”))
for n in range(start,end+1):
order=len(str(n))
sum=0
num=n
while(num>0):
rem=num%10
sum=sum+(rem**order)
num=num//10
if(n==sum):
print(n)
n = input().split()
if n[0] == 0:
n[0] = 1
total = 0
for j in range(int(n[0]),int(n[1])+1):
total = 0
for i in str(j):
total += int(i)**3
if total == j:
print(j)
import math
def armstrong_no(n):
k = len(n)
sum = 0
for i in n:
sum+= pow(int(i), k)
if sum == int(n):
return 1
else:
return 0
a = int(input(“first number: “))
b = int(input(“second number: “))
c = []
for j in range(a, b+1):
j = str(j)
if armstrong_no(j) == 1:
c.append(j)
print(“armstrong numbers: ” + str(c))
n=input(“Enter first number:”)
x=input(“Enter second number:”)
z=0
count=0
for i in range(int(n),int(x)+1,1):
k=str(i)
d=len(k)
h=[l for l in k]
for c in h:
z=z+(int(c)**d)
if(z == int(i)):
print(i,”number is Armstrong”)
count=count+1
z=0
h.clear()
else:
print(i,”number is not Armstrong”)
z=0
h.clear()
print(count)
n1 = int(input(“Enter Lower range : “))
n2 = int(input(“Enter Upper range : “))
for i in range(n1, n2+1):
ord = len(str(i))
sum = 0
temp = i
while temp > 0:
digit = temp % 10
sum = sum + digit ** ord
temp = temp // 10
if i == sum:
print(i)