Calculation Problem | Sololearn: Learn to code for FREE!

+1

Calculation Problem

I wrote a code and find a problem in calculation, so I want to know how C calculates it. When I calculate it by BODMASS method I got k = 3, but C shows k = 2, why? https://code.sololearn.com/c6cqUOp4e6y4/?ref=app

10/1/2018 3:15:40 PM

Pritam Kumar Das

5 Answers

New Answer

+2

「HAPPY TO HELP」 j is 3 here. I think this is happening because we're working with integers. 3/2 is calculated as 1. The part after the decimal point is ignored. If we change the line to k = 2 * ((float) j / 2); it should work out.

+6

+5

+4

C has operator precedence, and everything inside parentheses goes first. Lets step by step this. k = NULL, j = 2 k = 2 * (j / 2) k = 2 * (2 / 2) // () first (2 / 2) // division = 1 k = 2 * 1 k = 2 Here's a table of operator precedence for you to see https://en.cppreference.com/w/c/language/operator_precedence

+2

「HAPPY TO HELP」 😊 It's the intention that matters. Your explanation was very detailed.