Sunday, 13 July 2014

Armstrong Number

Armstrong Number : Numbers who's sum of cube of digits of number is equal to same given number. Then number is called Armstrong Number .

Algorithm for Armstrong Number :


Concept of  Armstrong Number is basically Algorithm to find it . Find sum of cube of digit ,way of finding this may differ according to programming language you choose .

I use Python 2.7 So I can directly convert number into string and accessing character by character and hence find sum of cube of digits



Python Program to check Armstrong Number :


  1. #ArmStrong Number
  2. #http://beginer2cs.blogspot.com
  3. def is_armstrong(n):
  4.     temp = str(n) #convert to string
  5.     summ=0
  6.     for i in temp: #Find summ of cube of digits
  7.         summ=summ + (int(i)**3)
  8.     if summ==n:  #if sum of cube of digits == given number
  9.         return True
  10.     else:
  11.         return False
  12. if __name__=='__main__':
  13.     print '''
  14. Enter numbers you wanna check ,
  15. they must be seperated by space
  16. '''
  17.     ls=[int(x) for x in raw_input().split(' ')] #taking numbers as input
  18.     print "Is __ Amicable"
  19.     for i in ls: #for each number print ls
  20.         print str(i) +'   -->>  '+str(is_armstrong(i))


Output :
Armstrong Number in python
Add caption

C- Program to check Armstrong Number :

  1. //Armstrong Number in c
  2. // http://beginer2cs.blogspot.com
  3. #include<stdio.h>
  4. int main()
  5. {
  6. int num,temp,sum=0,rem;
  7. printf("nEnter Number to Checking Armstrong : ");
  8. scanf("%d",&num);
  9. temp = num;
  10.  while (num != 0)
  11.  {
  12.   rem = num % 10;
  13.   sum = sum + (rem*rem*rem);
  14.   num = num / 10;
  15.  }
  16. if(temp == sum)
  17.     printf("n%d is Amstrong Number",temp);
  18. else
  19.     printf("n%d is Amstrong Number",temp);
  20. return(0);
  21. }


Thanks :)

1 comment:

  1. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Data Science with Python , kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on TECHNOLOGY. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us.
    Sangita Mohanty
    MaxMunus
    E-mail: sangita@maxmunus.com
    Skype id: training_maxmunus
    Ph:(0) 9738075708 / 080 - 41103383
    http://www.maxmunus.com/

    ReplyDelete

THANKS FOR UR GREAT COMMENT

Blogger Widgets