Check whether the given number is Armstrong


Check whether the given number is Armstrong

Note:(asked question)
negative armstrong number,
strong number in python,
armstrong numbers between 1 to 1000

Define a function that check whether the given number is Armstrong.

Aim: A function that check whether the given number is Armstrong

Algorithm:                                                                                                                                   

Step1. Start

Step2. import math

          Declare the variable n and read n

Step3. define armstrong function

Step4. In armstrong function

   Assign n to a

Initialise sum to 0

Step5. In while Loop, perform

            d=n%10

          sum+=d*d*d

          n=math.floor(n/10)

Step6. check the condition    sum==a

 Step7  If the condition is true print that given number is Armstrong number

Step8. otherwise print that given number is not Armstrong number

Step9. Call armstrong (n)  function

Step10. End

Program:

import math

n=int(input(“enter the number:”))

def armstrong(n):

    a=n              # 0  1  153  370   371   407

    sum=0

    while(n!=0):

             d=n%10

            sum+=d*d*d

             n=math.floor(n/10)

    if(sum==a):

       print(“given number is Armstrong number”)

    else:

      print(“given number is not Armstrong number”)

armstrong(n)

Ouput:

>>>

enter the number:245

given number is not Armstrong number

>>>

>>>

enter the number:153

given number is Armstrong number

>>> 

Recommended Posts:

Check whether the given number is Armstrong

Have any Question or Comment?

Leave a Reply

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

Categories