#include<stdio.h>
#include<math.h>
int main(){
int num,onum,numl=0,tem,nnum=0;
printf("Enter num: ");
scanf("%d", &num);
onum =num;
while (num!=0){
num = num/10;
++numl;
}
num = onum;
while (num!=0){
tem = num%10;
nnum += pow(tem,numl);
printf("%d",nnum);
num /= 10 ;
}
if (nnum == onum){
printf("is");
}
else{
printf("not");
}
}
works fine with other Narcissistic numbers .
but when input 153 , 5**3 is mistakenly counted to 124, why?
This question is duplicate of many questions. And the answer below is the most easy-to-understand anwer.
In one sentence, the inaccurate calculation is caused by {inaccurate {data type usage} or {compiler} or {FPU} }.
I avoided the error by updating my compiler to gcc 7.2 . But solving the problem needs a pow function for integer.