question:https://hack.codingblocks.com/contests/c/509/195
answer:https://ide.codingblocks.com/#/s/25285
Basic calculator time limit exceeded
The numbers can be both positive and negative. So remove that if condition.
And you need to take input of numbers only when the character is +,-,*,/,%
Otherwise you don’t have to take input of numbers from user.
Make these changes in your code after reading the question again carefully.
In constraints column there is written (Numbers should be greater than 0 and lesser than 100000000.)
These constraints mean that the number entered would be in this range.
You need not check these conditions in he question.
@ayushi773478
Your code does not work for input even like
+
2
3
x
You need to take input numbers n1 and n2 whenever user enters valid operator.
Please reattempt after reading the question carefully again.
https://ide.codingblocks.com/s/40760
Why does it keep printing “invalid operation try again” indefinitely ?
Hey AKshay, your way off writing this code is wrong as after taking input sign then you immediately takes the input a and b without checking which sign is this. Have a look at this example
for eg.
(
+
3
2
;
X
in this case, according to your code input will be taken as
1 ->
sign = (
a = +
b = 3
2 ->
sign = 2
a = ;
b = X
So, in this way sign will not get the input ‘X’ or ‘x’ and loop will never break which will result in an infinite loop printing “invalid operation try again”.
To correct this first check which sign are you getting in input and if the sign is a valid one then only input the numbers a and b.