+ 7
In C, if a=300*300/300; then what is output of printf("%d",a);
Hint:Run the program turbo c and see the actual result https://code.sololearn.com/ceqdzl96VJ37/?ref=app
47 Respostas
+ 19
You're probably using a two byte integer to store the answer. A little more detail about what it's probably doing:
300 = 0000000100101100 (9 bits required)
300 * 300 = 90000 = 10101111110010000 (17 bits required! Uh oh!)
So, because it won't fit into 16 bits, you lose the 1st bit, which is set to a 1. You might actually be losing the 1st two bits, if it's a signed short value, but we can't be sure. Anyway, your result gets truncated into 16 bits:
0101111110010000 = 24464
Next, when you divide by 300, you get:
24464 / 300 = 81.54666...
And since it's not a floating point number, it's truncating it to 81. You may be able to avoid this problem by changing your calculation to:
300*(300/300)
That's mathematically the same, but won't overflow the bytes.
this question is Google interview question
you also can check the answer in google
+ 5
Here operator * and / have same precedence but here associative will be from left to right... first 300 * 300 then 90000 / 300 then your output is 300.
+ 5
*/+-
First multiply 300 and 300 then divided it with 300.
300*300=90000/300=300
+ 3
Vachaspati Annaldas
let's try this another way...
try using pencil and paper
leave your turbo c off...
+ 2
Vachaspati Annaldas where in your question or code does it say anything about converting to bits ???
https://ibb.co/5ckx8v2
+ 2
i now see your argument and instead of creating this thread you should have explained this as a direct question from stackoverflow... again comparing apples and oranges so both answers are correct ...
https://stackoverflow.com/questions/7106702/gcc-and-tc-giving-different-outputs
+ 2
Pathik Patel there's no mathematics rule that says divide first - if both are present in an equation, you do it from left to right.
+ 2
Try a=200/300*300 and see if the answer is what you expect.
+ 2
self answered questions that was really who marked it as best ; a great individual he/she might be
+ 1
Vachaspati Annaldas But it's still 300
+ 1
No, it isn't, I don't see how it would ever equal 81, this really complicated equation has an obvious answer.
+ 1
Answers=300
As mathematics rules first divide second multiply third addition and last subtraction
300/300 =1
300*1=300
+ 1
This code result is 81 use C basic concept not use the maths concept.
Hint: To Multiplication and Division use binary value of 300
+ 1
Vachaspati Annaldas verses repeating this over and over - show us an actual screenshot with code and answer displayed... otherwise this discussion is mute.
All you have proven so far is SoloLearn has the right answer and turbo c is flawed...
300 * 300 = 90000 / 300 = 300
Really simple math using the basics of pedmas left to right and even going from right to left which is not normal...
300 / 300 = 1 * 300 = 300
+ 1
Well, I don't think I would say, "so both answers are correct." Both answers are what the programs compiled by the respective compilers would give you. However, I think we'd agree that the truly correct answer is what mathematics say (300). You can blame Turbo C's erroneous answer on it not using enough bits for int or the programmer for not accounting for a possible overflow and thus not using the right type. That's the view from where I sit.
+ 1
Thanks for the question (and solution), it was quite interesting. I would just be careful with saying 81 is the right answer. The right answer, as said by many here, is 300. The compiler gives you the wrong answer due to the overflow...
+ 1
The output is 300 with return 0; the * is take Preference ✌
+ 1
Stdio first answer
+ 1
300
0
It works just fine, 300 ^ 2 / 300 ^ 1 equals 300 ^ (2 - 1), which is 300 in my opinion.