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 :)
No comments:
Post a Comment
THANKS FOR UR GREAT COMMENT