0
Where am I wrong?
#include <iostream> #include <cmath> using namespace std; int main() { int num,temp,sum=0,n=0; cin>>num; temp=num; while (temp>0) { n++; temp/=10; } temp=num; while (temp>0) { sum=sum+pow(temp%10,n); temp/=10; } if(sum==num) cout<<"Ok"; else cout<<"No"; return 0; }
14 Antworten
+ 14
#include<iostream>
+ 5
Thats not just the fault of the Sololearn Compiler, but is in fact a problem with pow() in general.
You see, for some multiples of 5, if you try printing:
cout<<pow(5,3);
// prints 124, not 125.
cout<<pow(5,4);
prints 3024 not 3025.
The reason for these results is that the power function is bound to use float, and so it casts the number. Accidentally, with 5, the product generated is 124.99999 and the output gets floored. Thus, your program returns incorrect values.
So, I suggest you try using a*a*a instead of pow() for integers.
https://www.sololearn.com/discuss/173437/?ref=app
+ 5
@Gordie
Well, I use GCC 6.4 and still get 124 whenever I try pow(5,3).
+ 4
@Gordie
Maybe they corrected it? I don't know.
And do you directly print the pow(5,3) operation? Try saving it to an int and then print that int. Do you still get the same result?
+ 2
ohh...it makes me sense...I will try later Thank you all!
+ 1
no, bro. It's just typing error. i m facing with logical error. when I typed 153, it showed no. but 153 is Amstrong number. Anyway, thank you for your answer
+ 1
so, it seems my app error. lol...it also ok when I typed in my computer. Anyway Thank you for you answer.
0
ofc bro, I really appreciate! it's work now after changing datatype of sum. Do me a favor. if you have line id, could i get it?