Armstrong Number

Armstrong Number

If the sum of the cubes of  the digits in a three digit number is equal to the number itself, then the number is called an Armstrong number.
E.g.: 153 is an Armstrong number because (13)+(53)+(33) = 153.

Write a program in Python to display all the  Armstrong numbers between n1 and n2.

Note:

  1. If the starting and ending numbers are negative then display the message “Starting and ending numbers must be greater than or equal to zero” and stop the program. 
  2. If the starting number is greater than the ending number, then display the message “Invalid input!! Ending number should be greater than starting number” and stop the program. 
  3. Refer the sample input and output statements for more clarifications.

Sample Input 1:

Enter the starting and ending numbers:

5 500

Sample Output 1:

Armstrong numbers between 5 and 500 are:

153

370

371

407

Sample Input 2:

Enter the starting and ending numbers:

50 100

Sample Output 2:

Armstrong numbers between 50 and 100 are:

There is no Armstrong number between these numbers

Sample Input 3:

Enter the starting and ending numbers:

1 10

Sample Output 3:

Armstrong numbers between 1 and 10 are:

1

Sample Input 4:

Enter the starting and ending numbers:

-4 2

Sample Output 4:

Starting and ending numbers must be greater than or equal to zero

Sample Input 5:

Enter the starting and ending numbers:

100 6

Sample Output 5:Invalid input!! Ending number should be greater than starting number

Code :-

x,y=map(int,input("Enter the starting and ending numbers:\n").split(" "))
l=[]
if x>=0 and y>=0 and x<y:
    print("Armstrong numbers between "+str(x)+" and "+str(y)+" are:")
    for i in range(x,y+1):
        s=0
        c=0
        temp=i
        while temp>0:
            digit=temp%10
            s+=digit**3
            temp//=10
        if i==s:
            l.append(i)
    if len(l)>0:
        for i in l:
            print(i)
    else:
        print("There is no Armstrong number between these numbers")
elif(x<0 or y<0):
    print("Starting and ending numbers must be greater than or equal to zero")
elif(x>=0 and y>=0 and x>=y):
    print("Invalid input!! Ending number should be greater than starting number")

1 thought on “Armstrong Number”

Leave a Comment

Your email address will not be published. Required fields are marked *