What is the answer for 4%x where 'x' is a factor of 4 or is 0? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 3

What is the answer for 4%x where 'x' is a factor of 4 or is 0?

29th Dec 2016, 10:55 AM
Chirag Bhansali
Chirag Bhansali - avatar
6 Antworten
+ 3
is 4/0 also NaN?
29th Dec 2016, 11:13 AM
Chirag Bhansali
Chirag Bhansali - avatar
+ 2
what about in JavaScript
29th Dec 2016, 11:05 AM
Chirag Bhansali
Chirag Bhansali - avatar
+ 2
python ---- 0
29th Dec 2016, 11:17 AM
Dilan Perera
Dilan Perera - avatar
+ 1
In python: If you apply X % Y operation and Y is a factor of X, then it returns 0. That means if you keep removing Y from X you will get solid 0 at a point. When Y = 0, then it gives ZeroDivisionError. Think about it this way, no matter how many times you remove 0 from a number, it will still be that number. :) Similar things happen in other languages. Maybe the error will be of different type. For example: in Java ArithmeticException is thrown when Y is 0 in X % Y. However in C++ unless you try to use the value of X % Y (Y = 0) it won't cause any trouble. But the moment you try to print or use the value of X % Y where Y = 0, run time error will take place.
29th Dec 2016, 11:04 AM
Mahir Asef Kabir
Mahir Asef Kabir - avatar
+ 1
In JavaScript if you try to assign X % Y to a value where Y = 0, the assigned value is Nan. If Y is not 0 then it behaves same way as in other languages. For example: If the following code is written, var x = 4 % 0; Then x = NaN
29th Dec 2016, 11:10 AM
Mahir Asef Kabir
Mahir Asef Kabir - avatar
+ 1
For 4 / 0 its Infinity for JavaScript. You can easily test it in your browser. I am working on Google Chrome. In Google Chrome, go to inspect element (CTRL + SHIFT + I). Go to Console Tab. Test your JavaScript code there. :)
29th Dec 2016, 11:21 AM
Mahir Asef Kabir
Mahir Asef Kabir - avatar