Arshiya is just three and she already started programming. She has learned how to write loops. Recently, she found a simple problem written in the exercises segment of her programming book. The problem says, “Given a positive integer N, find the number of odd numbers from 1 to N inclusive”. Arshiya thought to herself, “It’s a piece of cake” and wrote down the following code to solve the problem:
Ok…it is a range from 1 to n inclusive and at least half of them will be odd and half of them will be even! now if the range is odd i.e. my res variable is odd then odd number will be more unless exactly half of the number will be odd.
I hope you are not regarding my theory as a trash .
The problem is not in your theory but how you are implementing it. In an inclusive range if you deduct the lower value fron the upper value sometimes you might lose the lower value itself! Let me give you an example. Assume that you want to know the number of number in the range 2 to 4 inclusive. Now there are 3 numbers which are 2, 3 and 4. However, if you do 4 - 2 then the answer would be 2! Meaning that you are losing a number in your calculation!
You have done the same thing here. Check if you have lost any number in your calculation.