Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- http://mathworld.wolfram.com/NarcissisticNumber.html
- you should take a look at this, coz in general, our idea of armstrong numbers are wrong
- we only look at base 3 narcissistic numbers as "armstrong" numbers
- 153(base 3 narcissistic) = 1^3 + 5^3 + 3^3
- 1634(base 4 narcissistic) = 1^4 + 6^4 + 3^4 + 4^4
- and so on, into the higer bases.
- ps:
- since we deal with base 3 narcissistic numbers as "armstrong" numbers, there are 4 such "armstrongs" only >> 153, 370, 371, 407
- */
- #include<stdio.h>
- int main(){
- int ip=0,index=0,temp=0,sum=0,rem=0,bool=0,narBase=0;
- do{
- printf("enter the number: ");
- scanf("%d",&ip);
- printf("enter the narcissistic base: ");
- scanf("%d",&narBase);
- temp=ip;
- for(index=0,sum=0;temp!=0;index++){ // since the whole thing is inside a do-while loop, its the zero initializations are needed
- printf("loop number %d\n",(index+1));
- sum+=myPow((temp%10),narBase);
- temp = temp/10;
- }
- printf("\n\ninput number: %d sum: %d\n",ip,sum);
- if(sum==ip){
- printf("hence %d is an narcissistic number of base %d",ip,narBase);
- }
- else{
- printf("hence %d is not a narcissistic number of base %d",ip,narBase);
- }
- printf("\n\niterate again for new number?\n yes(1) or no(0)\n enter choice: ");
- scanf("%d",&bool);
- }while(bool==1);
- return 0;
- }
- int myPow(int number, int exponent) // this is FAR from the fastest method outthere, but it gives u an idea
- {
- printf("number: %d exponent: %d\n",number,exponent);
- int index=0,temp=0;
- for(index=1;index<=exponent;index++){
- if(index==1)
- temp=number;
- else
- temp=temp*number;
- }
- printf("returned number: %d\n",temp);
- return temp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement