Suppose we have a floating point number; we have to check whether the number is odd or even. In general, for integer it is easy by dividing the last digit by 2. But for floating point number it is not straight forward like that. We cannot divide last digit by 2 to check if it is odd or even.
So, if the input is like n = 200.290, then the output will be Odd though the last digit is divisible by 2.
To solve this, we will follow these steps −
- s := convert number as string
- flag := False
- for i in range size of s - 1 to 0, decrease by 1, do
- if s[i] is '0' and flag is False, then
- go for next iteration
- if s[i] is same as '.', then
- flag := True
- go for next iteration
- if s[i] is even, then
- return "Even"
- return "Odd"
- if s[i] is '0' and flag is False, then
Let us see the following implementation to get better understanding −
Example Code
def solve(n) : s = str(n) flag = False for i in range(len(s) - 1, -1, -1) : if s[i] == '0' and flag == False : continue if s[i] == '.': flag = True continue if int(s[i]) % 2 == 0 : return "Even" return "Odd" n = 200.290 print(solve(n))
Input
200.290
Output
Odd