SHOW:
|
|
- or go back to the newest paste.
| 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; |
| 18 | + | int ip=0,index=0,temp=0,sum=0,rem=0,bool=0,narBase=0; |
| 19 | ||
| 20 | do{
| |
| 21 | printf("enter the number: ");
| |
| 22 | scanf("%d",&ip);
| |
| 23 | printf("enter the narcissistic base: ");
| |
| 24 | scanf("%d",&narBase);
| |
| 25 | ||
| 26 | temp=ip; | |
| 27 | ||
| 28 | - | rem = (temp%10); |
| 28 | + | |
| 29 | - | sum+= rem*rem*rem; |
| 29 | + | |
| 30 | ||
| 31 | sum+=myPow((temp%10),narBase); | |
| 32 | temp = temp/10; | |
| 33 | } | |
| 34 | ||
| 35 | printf("\n\ninput number: %d sum: %d\n",ip,sum);
| |
| 36 | - | printf("hence %d is an armstrong number",ip);
|
| 36 | + | |
| 37 | if(sum==ip){
| |
| 38 | printf("hence %d is an narcissistic number of base %d",ip,narBase);
| |
| 39 | - | printf("hence %d is not an armstrong number",ip);
|
| 39 | + | |
| 40 | else{
| |
| 41 | printf("hence %d is not a narcissistic number of base %d",ip,narBase);
| |
| 42 | } | |
| 43 | ||
| 44 | printf("\n\niterate again for new number?\n yes(1) or no(0)\n enter choice: ");
| |
| 45 | scanf("%d",&bool);
| |
| 46 | ||
| 47 | }while(bool==1); | |
| 48 | ||
| 49 | return 0; | |
| 50 | } | |
| 51 | ||
| 52 | ||
| 53 | int myPow(int number, int exponent) // this is FAR from the fastest method outthere, but it gives u an idea | |
| 54 | {
| |
| 55 | printf("number: %d exponent: %d\n",number,exponent);
| |
| 56 | int index=0,temp=0; | |
| 57 | ||
| 58 | for(index=1;index<=exponent;index++){
| |
| 59 | if(index==1) | |
| 60 | temp=number; | |
| 61 | else | |
| 62 | temp=temp*number; | |
| 63 | } | |
| 64 | printf("returned number: %d\n",temp);
| |
| 65 | return temp; | |
| 66 | } |