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; }

5th Oct 2017, 2:03 PM
Min Khant
Min Khant - avatar
14 odpowiedzi
+ 14
#include<iostream>
5th Oct 2017, 2:21 PM
Frost
Frost - avatar
+ 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
7th Oct 2017, 5:03 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 5
@Gordie Well, I use GCC 6.4 and still get 124 whenever I try pow(5,3).
7th Oct 2017, 5:14 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 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?
7th Oct 2017, 5:30 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 2
ohh...it makes me sense...I will try later Thank you all!
7th Oct 2017, 5:17 AM
Min Khant
Min Khant - avatar
+ 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
5th Oct 2017, 2:26 PM
Min Khant
Min Khant - avatar
+ 1
so, it seems my app error. lol...it also ok when I typed in my computer. Anyway Thank you for you answer.
5th Oct 2017, 2:52 PM
Min Khant
Min Khant - avatar
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?
5th Oct 2017, 3:20 PM
Min Khant
Min Khant - avatar