We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Wrong Code uses integer arithmetic ($((sum + num))), which truncates floating-point numbers, leading to incorrect results when decimals are involved. For example, with inputs 1.5, 2.5, and 3.0, the sum becomes 6, resulting in an incorrect average of 2.000.
Correct code uses bc for all arithmetic, which handles both integers and floating-point numbers correctly. With the same inputs, the sum becomes 7.0, and the correct average of 2.333 i
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Compute the Average
You are viewing a single comment's thread. Return to all comments →
Wrong Code uses integer arithmetic ($((sum + num))), which truncates floating-point numbers, leading to incorrect results when decimals are involved. For example, with inputs 1.5, 2.5, and 3.0, the sum becomes 6, resulting in an incorrect average of 2.000.
Correct code uses bc for all arithmetic, which handles both integers and floating-point numbers correctly. With the same inputs, the sum becomes 7.0, and the correct average of 2.333 i