I am getting a time limit error please help.
Basic Calculator Problem time limit error
Hi @Rooopak_Sharma, pls modify your code a little bit in order of execution of your algo. I have done some changes here. If you want have a look at them here.
Hope this helps
@Rooopak_Sharma Your code is correct except few silly mistake.
#include
using namespace std;
int main() {
char ch;
cin>>ch;
while(ch!='x' || ch!='X') // Do you have to check for 'or' or 'and'? Think.
{
if(ch=='+' || ch=='-' || ch=='*' || ch=='/' || ch=='%'){
int n1,n2;
cin>>n1>>n2;
if(ch=='+'){
cout<<n1+n2<<endl;
}else if(ch=='-'){
cout<<n1-n2<<endl;
}else if(ch=='*'){
cout<<n1*n2<<endl;
}else if(ch=='/'){
if(n2!=0){
cout<<n1/n2<<endl;
}
}else if(ch=='%'){
if(n2!=0){
cout<<n1%n2<<endl;
}
}
}
else if(ch!='x' && ch!='X') // Are you sure? Is this a correct condition. Do we need else or else if?
{
cout<<"Invalid operation.Try again."<<endl;
}
cin>>ch;
}
return 0;
}
I have commented the statements(only 2) that are causing TLE
Thanks Shub Bansal, Virender Singh sir for clarifying my doubt.