0
how do i optimize this code?
#include<iostream> using namespace std; int main() { int a, orig, rem=0,arm=0; cin>>a; orig==a; while(a!=0) { rem=a%10; arm=arm+(rem*rem*rem); a=a/10; } if(arm==orig) cout<<”yes"; else cout<<"no"; return 0; } error was you program took more time then expected time expected timelimit was <0.6144 seconds please optimize this code.
5 Antworten
+ 8
Instead of `a!=0` use this condition: `a>=0`
Because in many cases, it can be possible that `a` never becomes equal to 0. So instead of that, you can check if `a` become smaller than 0, that must happen in every case.
And as Anna has pointed out, you are using == instead of =. So this means, orig is hanging around with garbage value. Which leads us to the fact that your code will always print "no".
+ 6
orig=a; (instead of ==)
+ 1
thanks Anna and Zlytherin ,actually i did use assignment operator there instead of == and after changing a!=0 to a>=0 its still showing the same error , actual it regarding online test and it want an optimize code .
+ 1
its a program for checking an Armstrong number