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.

20th Mar 2019, 6:46 AM
Vishesh Saxena
Vishesh Saxena - avatar
4 odpowiedzi
+ 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".
20th Mar 2019, 6:50 AM
Letsintegreat
Letsintegreat - avatar
+ 6
orig=a; (instead of ==)
20th Mar 2019, 6:52 AM
Anna
Anna - avatar
+ 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 .
20th Mar 2019, 8:00 AM
Vishesh Saxena
Vishesh Saxena - avatar
+ 1
its a program for checking an Armstrong number
20th Mar 2019, 11:20 AM
Vishesh Saxena
Vishesh Saxena - avatar