How the output is 0?[solved] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
16th Mar 2021, 5:52 PM
Atul [Inactive]
17 Answers
+ 7
because 1 and 2 are int, and 1/more than 1 result to 0...
16th Mar 2021, 5:54 PM
visph
visph - avatar
+ 4
Because in C++ and C (and, maybe, in some other programming languages using separate data types for integers and floating-point/decimals, excluding Python) operators with integers return integers: 1 / 2 * 10 * 10 = 0 * 10 * 10 = 0 * 10 = 0.
16th Mar 2021, 5:55 PM
#0009e7 [get]
#0009e7 [get] - avatar
+ 4
try to put 1.0/2 to compute floating division, you'll get 50...
16th Mar 2021, 5:56 PM
visph
visph - avatar
+ 4
Or `(float) 1 / 2 * b * h` (the same as visph has written, but giving the converting 1 to float work to the compiler).
16th Mar 2021, 5:59 PM
#0009e7 [get]
#0009e7 [get] - avatar
+ 4
dividing int by int result to int dividing float by int or float give float
16th Mar 2021, 6:03 PM
visph
visph - avatar
+ 4
Atul, (continuing my first answer here); if one of the operands is float/double/long double (in C++ and C), the answer will be of this type. It was made for both precision and efficiency.
16th Mar 2021, 6:04 PM
#0009e7 [get]
#0009e7 [get] - avatar
+ 4
Atul If you have specific data types like int, double, float, long in any programming language like Java, C, C++, C# then in case of 1/2 you will always get 0 because here 1 and 2 will be considered as integer. But in case of some languages like Python, JS you will get in decimal.
16th Mar 2021, 6:36 PM
AอขJ
AอขJ - avatar
+ 3
18th Mar 2021, 8:53 AM
visph
visph - avatar
+ 2
So what should be done to make it 50?
16th Mar 2021, 5:56 PM
Atul [Inactive]
+ 2
Why after converting it into double it is showing the correct answer?
16th Mar 2021, 5:58 PM
Atul [Inactive]
+ 2
Ok thank you
16th Mar 2021, 7:03 PM
Atul [Inactive]
+ 2
๐Ÿ†„โฟแต˜หขแต˜แตƒหก๐™ฅ๐™š๐™ง๐™จ๐™ค๐™ฃ expected output is 50, not 0.005... first is computed 1/2 wich result to 0 as both numerator and divisor are int (but 0.5 expected) then 0.5*10 = 5 then 5*10 = 50
18th Mar 2021, 8:35 AM
visph
visph - avatar
+ 1
Variable 'a' is declare as 'int' type so it having a capability to store the integer value. 1/2*10*10 ==0.005 But it contain only integer part of (0.005) that is zero(0).....โœŒ๏ธ
18th Mar 2021, 8:31 AM
Mohd Aman
Mohd Aman - avatar
+ 1
visph According to operator precedence * first execute then / will be execute.
18th Mar 2021, 8:52 AM
Mohd Aman
Mohd Aman - avatar
+ 1
Amit Brar rgb is stand for red,green,blue color.it's value very from 0 to 255. Where rgb(0,0,0)-->black And rgb(255,255,255)-->white
18th Mar 2021, 10:43 AM
Mohd Aman
Mohd Aman - avatar
18th Mar 2021, 5:06 PM
visph
visph - avatar
0
z=2*10*10 a=int(1/200)=int(0.05)=0 https://code.sololearn.com/cyiqR1014jXG/?ref=app
18th Mar 2021, 7:30 AM
Sanjay Kamath
Sanjay Kamath - avatar