Question
Check whether the given number is armstrong number or not in a given range.
A positive number is called armstrong number if sum of its own digits each raised to the power of the number of digits is equal to number
Enter the lower range
1
Enter the Upper range
500
Amstrong Numbers:
1
2
3
4
5
6
7
8
9
153
370
371
407
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
Java
Python
Java
import java.util.Scanner;
public class ArmstrongInRange
{
public static void main(String[] args)
{
int c=0,r,temp=0,noOfDigits=0,lowerRange=0,upperRange=0,i=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the lower range");
lowerRange=sc.nextInt();
System.out.println("Enter the Upper range");
upperRange=sc.nextInt();
System.out.println("Amstrong Numbers:");
for(i=lowerRange;i<=upperRange;i++)
{
c=0;
temp=i;
noOfDigits=Integer.toString(i).length();
while(temp>0)
{
r=temp%10;
temp=temp/10;
c=c+(int)(Math.pow(r,noOfDigits));
}
if(i==c)
{
System.out.println(i+" ");
}
}
}
}
Python
lowerRange=int(input("Enter the lower Value:"))
upperRange=int(input("Enter the upper Value:"))
print("Armstrong number between",lowerRange,"and",upperRange,":")
for i in range(lowerRange,upperRange+1):
temp=i
totalDigits=len(str(i))
"""
str(i) converted integer to string
len() gives length of string
so len(str(i)) combined gives total number of digits
"""
sum=0
while(temp>0):
digit=temp%10
sum=sum+digit**totalDigits
temp=temp//10
if(sum==i):
print(i,end=" ")