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 :
- #ArmStrong Number
- #http://beginer2cs.blogspot.com
- def is_armstrong(n):
- temp = str(n) #convert to string
- summ=0
- for i in temp: #Find summ of cube of digits
- summ=summ + (int(i)**3)
- if summ==n: #if sum of cube of digits == given number
- return True
- else:
- return False
- if __name__=='__main__':
- print '''
- Enter numbers you wanna check ,
- they must be seperated by space
- '''
- ls=[int(x) for x in raw_input().split(' ')] #taking numbers as input
- print "Is __ Amicable"
- for i in ls: #for each number print ls
- print str(i) +' -->> '+str(is_armstrong(i))
Output :
Add caption |
C- Program to check Armstrong Number :
- //Armstrong Number in c
- // http://beginer2cs.blogspot.com
- #include<stdio.h>
- int main()
- {
- int num,temp,sum=0,rem;
- printf("nEnter Number to Checking Armstrong : ");
- scanf("%d",&num);
- temp = num;
- while (num != 0)
- {
- rem = num % 10;
- sum = sum + (rem*rem*rem);
- num = num / 10;
- }
- if(temp == sum)
- printf("n%d is Amstrong Number",temp);
- else
- printf("n%d is Amstrong Number",temp);
- return(0);
- }
Thanks :)
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
ReplyDeleteMaxMunus 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/