Advertisement
defineSN

narcissistic/armstrong number check

Jan 19th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.22 KB | None | 0 0
  1. /*
  2. http://mathworld.wolfram.com/NarcissisticNumber.html
  3.  
  4. you should take a look at this, coz in general, our idea of armstrong numbers are wrong
  5. we only look at base 3 narcissistic numbers as "armstrong" numbers
  6.  
  7. 153(base 3 narcissistic) = 1^3 + 5^3 + 3^3
  8. 1634(base 4 narcissistic) = 1^4 + 6^4 + 3^4 + 4^4
  9.  
  10. and so on, into the higer bases.
  11. ps:
  12. since we deal with base 3 narcissistic numbers as "armstrong" numbers, there are 4 such "armstrongs" only >> 153, 370, 371, 407
  13.  
  14. */
  15.  
  16. #include<stdio.h>
  17. int main(){
  18.     int ip=0,index=0,temp=0,sum=0,rem=0,bool=0;
  19.    
  20.     do{
  21.         printf("enter the number: ");
  22.         scanf("%d",&ip);
  23.        
  24.         temp=ip;
  25.        
  26.         for(index=0,sum=0;temp!=0;index++){ // since the whole thing is inside a do-while loop, its the zero initializations are needed
  27.             printf("loop number %d\n",(index+1));
  28.             rem = (temp%10);
  29.             sum+= rem*rem*rem;
  30.             temp = temp/10;
  31.         }
  32.        
  33.         printf("\n\ninput number: %d  sum: %d\n",ip,sum);
  34.        
  35.         if(sum==ip){
  36.             printf("hence %d is an armstrong number",ip);  
  37.         }
  38.         else{
  39.             printf("hence %d is not an armstrong number",ip);
  40.         }
  41.        
  42.         printf("\n\niterate again for new number?\n yes(1) or no(0)\n enter choice: ");
  43.         scanf("%d",&bool);
  44.        
  45.     }while(bool==1);
  46.    
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement