Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

closed 1. What is the result of the execution of the following code:

+24 votes
asked May 12 by ANG YU NING Moe (450 points)
closed Jul 5 by Admin
2.Why does round(5 / 2) return 2 instead of 3?  (BRAIN)
>>> round(7 / 2)
>>> round(3 / 2)
>>> round(5 / 2)
closed with the note: answered

10 Answers

+3 votes
answered May 12 by ANG YU NING Moe (450 points)
reshown May 13 by ANG YU NING Moe
Best answer

Answer 1: 4, 2, 2

Answer 2:The issue here is that Python’s round method implements banker’s rounding, where all half values will be rounded to the closest even number.

commented May 13 by ANG YU NING Moe (450 points)
0 votes
answered May 16 by Ralph Stallworth (140 points)
the answer is 2.5 so if its returning an integer, it will disregard everything after the decimal point
0 votes
answered May 16 by Hope (300 points)

the round() function rounds the quotients of 7/2 and 3/2 to the nearest integer, which is 3 and 1, respectively. However, the quotient of 5/2 is rounded to 2. This is because the quotient 2.5 is closer to 2 than it is to 3.

0 votes
answered May 24 by Thivenndhrah gundarapu (150 points)
answer 4,3,3
commented May 30 by ANG YU NING Moe (450 points)
thx for answering :>
+1 vote
answered May 25 by 215215109 (160 points)
Answers: 4 2 2

the round() function performs rounding based on certain rules. In the case of half-values (e.g., 2.5), it rounds to the nearest even number. This behavior is known as "round half to even" or "rounding to nearest, ties to even."
0 votes
answered May 26 by Chime Nwaru (140 points)
because when you divide integers the outputted value is the floor (rounded down answer)
0 votes
answered May 26 by Neetesh Ahirwar (140 points)
because integer / integer is always be a integer

like 5/2 is integer /integer then answer is integer it remove the number after the point and showing only who's number are before point
0 votes
answered May 29 by Abdul Hayee (180 points)
The result of the execution of the following code is:


The round() function rounds a number to the nearest integer. In the first two cases, the result is the nearest integer to the quotient of the two numbers. In the third case, the quotient, 2.5, is halfway between 2 and 3. The round() function rounds halfway cases to the even number, so the result is 2.

The round() function uses a rounding algorithm called banker's rounding. Banker's rounding is a rounding algorithm that rounds halfway cases to the even number. This algorithm is often used in financial applications because it is considered to be more fair than other rounding algorithms.
0 votes
answered Jun 23 by jothimani M (140 points)

rounding option rounds to the next higer integer value if the number of digits for precision is zero. hence 5/2 is only 3.  not 2




0 votes
answered Jun 28 by Sanjana Akula (140 points)
When we divide 5 with 2 we can get the answer which is in the decimal format that is 2.5.  So if the data type is int for the variable it prints 2 and removes decimal value 0.5.  Hence, the answer is 2 instead of 3
commented Jul 5 by meenu parmar (100 points)
moved Jul 5 by meenu parmar



it doesn't consider decimal value

as 7/2 gives 3 as quotient and 1 as a remainder.

so, output will be 3..

for 5/2 =2   and  3/2=1
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.